35.28 Rename Photos

Here we rename the filenames of the photos in our collection of photos using a standard format of four a digit year, two digit month, two digit day, then hour, minutes and seconds, and then a sequential count starting at 00, each segment separated by an underscore:


For a folder of photos we can find those that do not match the naming scheme with the regular expression below. This find command identifies regular files (-type f), using a sed type regular expression. The -regex here matches any prefix (path) and then 8 digits, underscore, 6 digits, underscore, two digits and then the file type.

$ find . -type f -regextype sed -not -regex '.*[0-9]\{8\}_[0-9]\{6\}_[0-9]\{2\}\.jpg'

We can use exiftool from the exiftool package to rename image files based on the meta data contained within the file itself.

First test the renaming that will take place. The %%-c will add -1, for example, if the new file already exists and %%le converts the original extension to lowercase.

$ exiftool -d %Y%m%d_%H%M%S_00%%-c.%%le "-testname<CreateDate" IM* 

Then do the actual renaming, adding a verbose option -v and changing -testname to -filename:

$ exiftool -v -d %Y%m%d_%H%M%S_00%%-c.%%le "-filename<CreateDate" IM*

If the CreateDate is not present the FileModifyDate might be:

$ exiftool -v -d %Y%m%d_%H%M%S_00%%-c.%%le "-filename<FileModifyDate" IM*

Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.