How do I print the image Exif date with a tool like exiv2?

My goal is to write the image year and month into separate variables. Do I really have to parse the output with regex or is there a alternative to something like this:

exiv2 DSC_01234.NEF -ps | grep 'Image timestamp' | ...regex to parse the date

You can use the -g flag to output only the property you're interested in, and -Pv to print the value without any surrounding fluff. The result is easy to parse.

IFS=': '
set $(exiv2 -g Exif.Image.DateTime -Pv DSC_01234.NEF)
unset IFS
year=$1 month=$2 day=$3 hour=$4 minute=$5 second=$6

It may also be helpful to change the file date to match the image date: exiv2 -T DSC_01234.NEF.

  • That's perfect. I'm trying to improve my bash knowledge while writing a script that creates date based folders for the images and renames the files appropriately like you mentioned. – apparat Mar 14 '11 at 22:12
set -- $(exiv2 -g Exif.Image.DateTime -Pv DSC_01234.NEF)
year=$1; month=$2; day=$3; hour=$4; minute=$5; second=$6;
echo "$year:$month:$day $hour:$minute:$second"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.