Rename files and change timestamps according to EXIF data

Q I have a photo collection that has got out of hand - several gigabytes' worth. I need to organise them so I can get a good backup. Do you know of a program that will rename a file based on the EXIF date of the image and change the Modified Date of the file to the same EXIF date? My last attempt at a backup before I wiped my PC managed to set all the file dates to when the DVD was burned. Also, I've managed to get myself several duplicate images spread across my entire collection (yep, I really messed up), each with different filenames. Any idea how I could sort them (maybe with EXIF data again) without having to look at a few thousand photos? If it helps, I'm using Fedora 64-bit and I'm not scared of the command line.

A There are several programs capable of working with EXIF data. My favourite is ExifTool (www.sno.phy.queensu.ca/~phil/exiftool). ExifTool can read and manipulate just about any EXIF information, including extracting the Date/Time Original or Create Data EXIF tags. You can use this information to rename the files or change their timestamps. For example:

find -name '*.jpg' | while read PIC; do
DATE=$(exiftool -p '$DateTimeOriginal' $PIC |
sed 's/[: ]//g')
touch -t $(echo $DATE | sed 's/\(..$\)/\.\1/') $PIC
mv -i $PIC $(dirname $PIC)/$DATE.jpg
done

The first line finds all *.jpg files in the current directory and below. The next extracts the Date/Time Original tag from each file (you may need to use Create Data instead, depending on your camera) and removes the spaces and colons. The next line sets the file's timestamp to this date the horrible looking sed regular expression is necessary to insert a dot before the final two characters, because the touch command expects the seconds to be separated from the rest of the time string like this. The final command renames the file, using the -i option to mv in case two files have the same timestamp. This will stop any files being overwritten.

It's also possible to do this with most digital photo management software without going anywhere near a command line - DigiKam, KPhotoAlbum, F-Spot and GThumb all have options for manipulating files based on the EXIF data. The disadvantage of using these programs for this is that they generally only work on a single directory at a time, whereas the above shell commands convert all JPEG files in a directory and all of its sub-directories. If you have several gigabytes of photos in the same directory, your collection is more out of hand than renaming some files will fix! The solution to your duplicates problem is a program called fdupes (available from http://netdial.caribe.net/~adrian2/fdupes.html or as an RPM for FC6). This compares the contents of files, so it will find duplicates even if they have different names and timestamps.

fdupes --recurse ~/photos

will list all duplicate files in your photos directory. There are also options that you can use to delete the duplicates:

fdupes --recurse --omitfirst --sameline ~/photos | xargs rm

Be careful of any option that automatically deletes files. Run without deletion first so you can see what's going to happen.

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar