Sunday, March 20, 2011

Rename Photos With Exiftool

There are a lot of usage examples for the powerful command line interface utility called Exiftool by Phil Harvey http://www.sno.phy.queensu.ca/~phil/exiftool/ but it took me a while to figure out the exact syntax to accomplish what I wanted to do. The following renames files (you can specify all writable files in a directory name at the end of the command, a pattern based on wildcards like *.JPG, or even one specific file) to something like 2011-02-20_03-05-41-1_NIKON D40.JPG (where filename is composed of date, time, "-" counter (the "." in the "%%-.c" causes the counter to start display "0" for non-duplicate filenames), EXIF Model tag, and original extension. THIS COMMAND DOES NOT LEAVE THE ORIGINAL FILENAME (it is like a "mv" not a "cp") :

exiftool '-FileName<${DateTimeOriginal}_$Model.%e' -d %Y-%m-%d_%H-%M-%S%%-.c ./DSC_1703.JPG

Yes, the filename has a space in it because the EXIF Model tag has a space in it, but if you're like I am and don't want spaces, it's easy enough to cleanse all the spaces from your filenames (in this case, with underscores) using the following command:

rename 's/ /_/g' *.JPG

Other tags besides Model can be used, too; I happen to want my filenames with Model in them. Of course one can combine these into one shell script called something like rename-myphotos.sh. It could look something like this:

#! /bin/bash
#
# Rename photos to something like:
# 2011-02-20_03-05-41-1_NIKON D40.JPG where filename is composed
# of date, time, "-" counter (the "." in the "%%-.c" causes the
# counter to start display "0" for non-duplicate filenames), EXIF
# Model tag, and original extension. THIS COMMAND DOES NOT LEAVE THE
# ORIGINAL FILENAME (it is like a "mv" not a "cp")
#
# Can replace ./ at end with directory name, filename, or filename
# pattern with wilcards.
#
# REMOVE "-n" FROM "rename" AND ADJUST FILENAME PATTERN AT END OF
# THAT LINE AS NECESSARY

exiftool '-FileName<${DateTimeOriginal}_$Model.%e' -d %Y-%m-%d_%H-%M-%S%%-.c ./
rename -n 's/ /_/g' ./

1 comment:

  1. Hi, you can avoid the rename command:

    exiftool '-FileName<${DateTimeOriginal}_${Model;tr/ /_/;s/__+/_/g}.%e' -d %Y-%m-%d_%H-%M-%S%%-.c ./

    Other than replacing space chars, according to Harvey's explanation: "I might suggest adding a semicolon to filter out characters in the Make string that might be illegal in file names"

    Sorry for delay in response! :)

    ReplyDelete