A tiny shell script for renaming photos
After a long weekend away my camera folder looked like a drawer of loose keys: files named IMG_4021.JPG, DSC00318.JPG and a few that a phone had called something else entirely. I wanted them sorted by the moment they were taken, in folders by month.
The script
It relies on exiftool, which is in most package managers:
#!/bin/sh
# usage: sortphotos.sh /path/to/messy-folder
set -eu
src=${1:?folder needed}
exiftool -r -d "%Y/%m" \
'-Directory<DateTimeOriginal' \
'-FileName<${DateTimeOriginal}_%f.%e' \
-d "%Y-%m-%d_%H%M%S" "$src"
The first date format puts each picture in a year and month folder. The second prefixes the file name with the exact time, so names sort the same way as the moments they record.
Two cautions
Run it on a copy first. Files without a recorded date will be left alone, and you can list them afterwards with a simple search. I found a dozen screenshots hiding among the holiday photos that way.
← All notes