You have a folder of photos named IMG_0412.jpg, IMG_0413.JPG, DSC_2281.jpg. You want them named by when they were taken, like 2024-07-14 18.03.21.jpg, so they sort in order.
How do you do it?
You ask an AI, and get a loop with stat and mv. Every photo is named today. The file's date is when the file landed on your disk, by download, AirDrop or cp, not when the photo was taken.
You ask again, and get sips -g creation, which is built into macOS. It looks right on the first photo. But I checked: it reads the date the photo was last modified. A photo you edited last week gets last week's date.
The date taken is in the photo's EXIF data, as DateTimeOriginal. exiftool reads it. Then two photos from the same burst have the same second, and the second mv silently overwrites the first one. And you'd like to see the new names before anything is renamed.
You save it somewhere: a Markdown note, or a rename-by-date.sh file. Next time it's another folder, or you want a quick look first, and you have to find it and remember how to make it a dry run.
With cmdbook, the script has one place to live, and it gets a GUI for free.
Here's how
1. Save the script once. Create a command in cmdbook and paste the script (it's at the end).
2. Mark what changes. The folder, which date to use, and whether to just look first:
DIR="{{prompt:folder}}"
SOURCE={{prompt:date?taken|created|modified}}
DRYRUN={{prompt:dryrun?yes|no}}
taken is the date in the photo (or video). created and modified are the file's own dates, for files that have no photo date.
3. Click Run with dryrun = yes. Drag the folder onto the form, press Enter, and read what would happen:
Dry run: nothing is renamed. Run again with dryrun = no to rename.
Bob's photo.jpg -> 2023-12-31 23.59.59.jpg
clip.mov -> 2024-07-14 10.00.00.mov
IMG_0412.jpg -> 2024-07-14 18.03.21.jpg
IMG_0413.JPG -> 2024-07-14 18.03.21 (2).jpg
Screenshot.png -> 2026-11-21 10.27.48.png (no date taken, used the file's creation date)
Would rename 5, 0 already named by date.
Two photos from the same second get (2), and nothing is ever overwritten.
4. Run again with dryrun = no. Your folder and date choice are still filled in, so it's one change. Run it again later, and files that already have the right name are left alone.
That's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.
Try cmdbook here: printf.app/cmdbook
The full script (macOS, needs exiftool for "taken")
DIR="{{prompt:folder}}"
SOURCE={{prompt:date?taken|created|modified}}
DRYRUN={{prompt:dryrun?yes|no}}
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
if [ "$SOURCE" = taken ] && ! command -v exiftool >/dev/null; then
echo "exiftool not found. Install it: brew install exiftool"; exit 1
fi
cd "$DIR" || exit 1
[ "$DRYRUN" = yes ] && echo "Dry run: nothing is renamed. Run again with dryrun = no to rename."
TAKEN=$(printf '\n') # names given out in this run, so two files never get the same one
RENAMED=0; SAME=0
for F in *; do
[ -f "$F" ] || continue
# Extension: the part after the last dot, if it has a letter (".jpg", ".mp4"; not ".48" in "10.27.48")
case "$F" in *.*[A-Za-z]*) case "${F##*.}" in *[A-Za-z]*) EXT=".$(printf '%s' "${F##*.}" | tr 'A-Z' 'a-z')" ;; *) EXT="" ;; esac ;; *) EXT="" ;; esac
D=""; NOTE=""
if [ "$SOURCE" = taken ]; then
# When the photo or video was taken: EXIF DateTimeOriginal, or the video's CreateDate
# (stored in UTC, converted to local time).
D=$(exiftool -s3 -api QuickTimeUTC -d '%Y-%m-%d %H.%M.%S' -DateTimeOriginal -CreateDate "$F" 2>/dev/null | head -n 1)
case "$D" in [0-9][0-9][0-9][0-9]-*) ;; *) D=""; NOTE=" (no date taken, used the file's creation date)" ;; esac
fi
if [ -z "$D" ]; then
if [ "$SOURCE" = modified ]; then FMT=%Sm; else FMT=%SB; fi
D=$(stat -f "$FMT" -t '%Y-%m-%d %H.%M.%S' "$F")
fi
NEW="$D$EXT"; N=2
while [ "$NEW" != "$F" ] && { [ -e "$NEW" ] || printf '%s\n' "$TAKEN" | grep -Fxq "$NEW"; }; do
NEW="$D ($N)$EXT"; N=$((N + 1))
done
TAKEN="$TAKEN
$NEW"
if [ "$NEW" = "$F" ]; then SAME=$((SAME + 1)); continue; fi
echo "$F -> $NEW$NOTE"
[ "$DRYRUN" = yes ] || mv -n "$F" "$NEW"
RENAMED=$((RENAMED + 1))
done
echo
if [ "$DRYRUN" = yes ]; then echo "Would rename $RENAMED, $SAME already named by date."
else echo "Renamed $RENAMED, $SAME already named by date."; fi

Top comments (0)