I am trying to create a script using jhead
or exiftool
to help me fix the metadata issues I got due to an Apple photo export. I am trying to move away from the apple ecosystem but Apple photos didn't just use EXIF to sort photos but also import dates etc. So when I export original photos with EXIF data, they end up in directories containing the date but don't all have exif data.
For example:
/8 February 2011/img - 6676.JPG
Is the only photo in this directory that doesn't have metadata. I would like to add the date (8-2-2011) to the metadata.
Not very good with bash so if anyone can help, I would greatly appreciate.
Continuing from the comments, you can easily convert the directory name to a date using date -d
in bash. The trick is parsing the directory from the full filename (e.g. /8 February 2011/img - 6676.JPG
). Since it is an absolute path, you will have to use string indexes to chop the leading '/'
regardless whether you use the dirname
command or you simply parse it with bash parameter expansion (which is the simplest here).
Here is a short example showing how to handle the directory you have given. You may need to modify the jhead
commands so test it before you just turn any script loose on all of your photos.
#!/bin/bash
f="/8 February 2011/img - 6676.JPG" ## example filename "$f"
[ "${f:0:1}" = '/' ] && d="${f:1}" ## check/trim leading '/' for dir "$d"
d="${d%%/*}" ## strip filename leaving dir for time in $d
dt="$(date -d "$d" +'%d-%m-%Y')" ## get date from $d in your format
jhdt="$(date -d "$d" +'%Y:%m:%d')" ## get date in format required by jhead
## output dates and commands for jhead
printf "file : \"%s\"\n" "$f"
printf "has date: %s\n" "$dt"
printf "jhead -mkexif \"%s\"\n" "$f"
printf "jhead -ds %s \"%s\"\n" "$jhdt" "$f"
Example Use/Output
$ bash dirtodate.sh
file : "/8 February 2011/img - 6676.JPG"
has date: 08-02-2011
jhead -mkexif "/8 February 2011/img - 6676.JPG"
jhead -ds 2011:02:08 "/8 February 2011/img - 6676.JPG"