Skip to content

Instantly share code, notes, and snippets.

@bmarwell
Created April 28, 2018 09:50
Show Gist options
  • Save bmarwell/0d7bf2926db565833306e7aabdc73817 to your computer and use it in GitHub Desktop.
Save bmarwell/0d7bf2926db565833306e7aabdc73817 to your computer and use it in GitHub Desktop.
Rename JPG image files to a standardized iso format
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
NOOP=${1:-}
COUNTER=0
if [ "$NOOP" == "-n" ]; then
echo "Simulation mode. -n given."
fi
for file in "$@"; do
newname=$(exiftool -d "%Y-%m-%dT%H-%M-%S_$(printf %04d $COUNTER)" -DateTimeOriginal -S -s -q "$file")
let COUNTER=COUNTER+1
if [ "x$newname" == "x" ]; then
echo "Skipping file $file -- no date available." >&2
continue
fi
echo "[$file] => [$newname].jpg"
if [ "$NOOP" == "-n" ]; then
continue
fi
mv "$file" "${newname}.jpg" &
done
@bmarwell
Copy link
Author

bmarwell commented Apr 28, 2018

This bash script will convert the filenames of your current directory to a standardized ISO format:

  • 2018-04-28T11:50:00_0001.jpg

It uses its exif information. exiftool needs to be installed.

Pass -n for simulation.

Otherwise, cd to the dir you like to change and use:

~/bin/isorename.sh *.jpg *.JPG
# OR
~/bin/isorename.sh *.{jpg|jpeg|JPG|JPEG} # will also do.

Executing a 2nd time might change the counter variable, because it is not determined by date. So if you like to keep your cameras ordering, don't run it a 2nd time. I always run it a 2nd time, though :)

The counter is needed, because of camera's burst modes which will do many pictures in the same second. Otherweise images would overwrite each other.

HTH
I never lost any files, but use it at your own risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment