Recently I exported a whole bunch of files from Lightroom to a NAS share and filenames like 2E570434-67B7E0489CA2-39354-000017CF24DD8ACD.jpg are not very informative. It would’ve been nice if the filename contained some useful information like date, camera model, maybe even location.

The exiftool by Phil Harvey has been described as the Swiss Army knife for file metadata manipulation and it certainly is that. For my purposes, I only needed a small subset of the functionality offered by exiftool (which for some reason I keep misspelling as ‘exitfool’…)

Anyway, I needed the filename to contain the timestamp when the photo was originally taken, the camera model (as short as possible – just enough for me to identify the equipment I used), and the location where the photo was taken (provided the camera supported geotagging).

Now, since multiple photos could’ve been taken at roughly the same time, with the same camera, and at the same location, the filename would also need to contain some sort of numerical auto-incremental field. Here’s an example of my “perfect” filename for photos:

I thought including city, state, and country in the filename was sufficient for my needs. You don’t really want to make that filename too long.

The original filename contained no useful information, so I did not want it to be a part of the new filename. Having said that, I thought it might be a good idea to add the original filename as an Exif tag just for record-keeping purposes, but also so I can find it in the Lightroom catalog, should the need arise.

It should be noted, though, that adding a tag to a file requires rewriting the entire file, so it adds quite a bit of time to the renaming process. Feel free to drop this step if the original filename is not something you care to remember. And, by the way, if you want to see the available tags in a file, use this command: exiftool -a -G1 -s <FILENAME>

If your camera does not support geotagging or if you just don’t care to include location information in the filename, the renaming syntax is simple:

Now we get to the more complicated part: geotagging. You can extract the GPS coordinates from the photo using exiftool like so:

Converting latitude and longitude to a geographic name requires some sort of geolocation database. Unless you have one handy, I suggest you obtain an API key from one of the providers. Google is a popular choice, but I found Geocodio an easier process. With a free account you can do up to 2,500 daily lookups and you don’t need to provide payment information or, really, any personal details when you sign up for a free account.

Here’s a sample reverse geolocation lookup where you send the GPS coordinates and the API responds with the geographic name for that location:

So now that we have this, how to tie it together with exiftool file renaming process? Not complicated at all, actually.

First we rewrite the previous curl command to include the exiftool syntax that will dynamically read the GPS coordinates from the file:

And now we just take the previous renaming example, change single quotes to double-quotes for the -d section, and right after the 03.c bit insert the $(longuglycommand), where the command would be that curl syntax above.

The final step is to produce a loop to rename the files of your choice. Even though exiftool has the recursive option, for better control I would suggest using find. Here’s an example:

The time-consuming part here is the -overwrite_original_in_place that saves the original filename as a tag inside the file. As I previously mentioned, if you don’t want it – you don’t have to keep it. If you do want it, however, it may be possible to speed up the process by taking advantage of your computer’s multiple CPU cores.

Below is an example using xargs. Just keep in mind that the free geotagging API you might be using may rate-limit access, so you should probably do this parallel processing only if you have a paid account. You can also add the convert_function to your .bashrc so you can use it to rename individual files manually.

If you find yourself with just too many files in a single folder, you can use exiftool to build a year/month/day folder structure and move the file into those folders based on when each photo was taken. Here’s an example:

It may also help to create a contact sheet for the every month inside the year folder. Here’s an example that uses the montage command (a part of the imagemagick package).

And here I am, hopefully on the way to organizing my mess of a photo archive.