67.6 Photo Rename
20230316
Photos in a directory can be renamed using a standard format of choice. We might begin the filename with a four digit year, two digit month, two digit day, an underscore and then hour, minutes and seconds, another underscore, and then either a sequential count starting at 00 or some text describing the contents:
YYYYMMDD_HHMMSS_NN.jpg
YYYYMMDD_HHMMSS_TEXT.jpg
An egrep regular expression for this is
.*/[0-9]{8}_[0-9]{6}_([0-9]{2}|[a-z][a-z0-9_]*).jpg
Related photos may appear in an album named appropriately, like
family
or weddings
. Sub folders might then correspond to specific
events and named using the YYYYMMDD_
format, followed by a text
description of the event.
Often a directory of photos will contain photos that do conform to the style amongst others that don’t. The exiftool command can be used to rename image files based on the meta data contained within the file itself.
Our use case is to rename all jpg files in the current directory to conform to our preferred naming scheme.
As always, it is best to test the renaming before actually performing
the renaming. Using -testname
the renaming is reported but will not
take place. Once the renaming plan is confirmed, replace -testname
with -filename
.
The renaming we undertake is directed from the CreateDate field of the
image file’s meta data. We access this using the <CreateDate
argument. The date format to be used is introduced with -d
. The
%%.2c
will add 00
for the first instance of any date stamp, and
01
for replicated names, etc. The %%le
converts the original
extension to lowercase.
If the proposed changes are correct then do the actual renaming by
adding a verbose option -v
and changing -testname
to -filename
:
If the CreateDate is not present the FileModifyDate might be used instead:
Replace %%.2c
with %%f
to reuse the current filename after
pre-pending the date.
When using %%f
be sure not to rename files already named using your
schema otherwise the date will appear again each time you run the
command!
To operate on all files, remove the -ext jpg
, or replace jpg
with png, etc.
We can also find a specific set of files to operate on using the
find command. For example, we might find regular
files (-type f
) that match a pattern using an
egrep type regular expressions (-regextype egrep
). The -regex
in the example below matches any prefix
(directory path) and then 8 digits, an underscore, 6 digits, an
underscore, and then two digits. By adding a -not
the test is
reversed so that we find the files we would like to rename.
To test rename those files:
find . -type f -regextype egrep -not -regex '.*/[0-9]{8}_[0-9]{6}_([0-9]{2}|[a-z][a-z0-9_]*).*' |
xargs exiftool -v -d %Y%m%d_%H%M%S_%%.2c.%%le "-testname<FileModifyDate"
If those changes look okay replace -testname
with -filename
.
See Section 5.10 for details of regular expressions. To rename videos see Section91.16.
Your donation will support ongoing availability 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-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0