Documents‎ > ‎

Renaming JPEG photos using their EXIF data date and time

One thing that annoys me about digital photos is how most cameras use their own progressive numbering scheme to name files. I prefer the way dropbox names them when it imports photos: it gives a name like "2012-07-09 16.36.32.jpg", taking the date and time when the photo was taken from the EXIF information embedded in the JPEG format.

I made a small bash script which takes a JPEG file and renames it with its date and time.

First of all you have to get the jhead tool from here:
jhead will help you extract the EXIF information.

Then create a shell script with the following lines of code:

j=`jhead "$1" | grep "Date/Time" | sed 's/Date\/Time[ ]*: //' | sed 's/:/-/1' | sed 's/:/-/1' | sed 's/:/./g'`.jpg
echo "Renaming ""$1"" to ""$j"
mv -i "$1" "$j"

Suppose you call the script "exifrename", then you have to execute "exifrename somephoto.jpg" and it will rename "somephoto.jpg" to a jpeg with date and time in the name.
The best thing to do is to put it somewhere in your PATH (e.h., under /usr/local/bin) so that you can execute this script from anywhere.

Now, if you want to batch execute this script for a whole bunch of files, do the following. Say that you have a directory with several photos named "IMG_xxxx.JPG", where xxxx is a four-digit progressive number given by your camera. Open terminal, cd to the directory where the photos are and type the following:

for i in IMG_*.JPG; do "$i"; done

Note: the -i option will cause mv to ask you before overwriting files. This can occur if two different photos have been taken at the same time (down to the second) – the script will be trying to rename both to the same filename. With modern cameras, who can shoot several frames per second, this is all but infrequent. 

There. Now if I could find an easy way to do that for movies...