9

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
don_crissti
  • 82,805
apparat
  • 622

2 Answers2

7

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
0
OIFS=$IFS; IFS=': '
set -- $(exiv2 -g Exif.Image.DateTime -Pv DSC_01234.NEF)
IFS=$OIFS;
year=$1; month=$2; day=$3; hour=$4; minute=$5; second=$6;
echo "$year:$month:$day $hour:$minute:$second"
Dmitry
  • 1