How retrieve the creation date of photos with a script

i have written a bash script to copy files straight off my iphone/ipad and copy them into folders on a target drive based on image creation date. i use a program called exiftool found at http://www.sno.phy.queensu.ca/~phil/exiftool/

for a given image, i extract the creation datetime into an array using

DATEBITS=( $(exiftool -CreateDate -FileModifyDate -DateTimeOriginal "$IMGFILE" | awk -F: '{ print $2 ":" $3 ":" $4 ":" $5 ":" $6 }' | sed 's/+[0-9]*//' | sort | grep -v 1970: | cut -d: -f1-6 | tr ':' ' ' | head -1) )

where $IMGFILE is the path to the image file. you can then get access to the year, month, day etc using

YR=${DATEBITS[0]}
MTH=${DATEBITS[1]}
DAY=${DATEBITS[2]}
HR=${DATEBITS[3]}
MIN=${DATEBITS[4]}
SEC=${DATEBITS[5]}

it is then trivial to create the directory you need to stash the image file in


According to exiftool man page 'Renaming examples'

exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d "$dir"

and if only copying is required, there is an option:

exiftool -o . '-Directory<DateTimeOriginal' -d %Y/%m/%d "$dir"

has the same effect as above except files are copied instead of moved.