Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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
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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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