When exporting photos and videos from smartphones, cameras, or older backups, you almost always end up with a mix of inconsistent filenames (IMG_20200612.jpg, DSC04512.ARW, VID-20230410-WA0002.mp4) that make clean chronological sorting difficult.
Relying on filesystem modification times (mtime) is risky: simply copying files to an external drive, downloading them from cloud storage, or moving them between folders can alter their timestamps. The only reliable source is the embedded metadata (EXIF and QuickTime/MP4) written when the photo was taken or the video was recorded.
This Bash script automatically renames photos and videos to the YYYYMMDD_HHMMSS.ext format (for example, 20200612_205732.jpg), preserving the original file extension and protecting files against accidental overwrites.
What the Script Does
- Controlled recursion (
-r/--recursive): Without-r, it only processes the top level of each target directory (-maxdepth 1); with-r, it descends into subdirectories. If no paths are provided, it defaults to the current directory. - Timestamp tag chain: Queries
exiftoolin order forDateTimeOriginal,CreateDate, andMediaCreateDate, picking the first valid value (helpful for both still photos and many video formats). - Strict format validation: Accepts only timestamps matching
YYYYMMDD_HHMMSS(^[0-9]{8}_[0-9]{6}$), ignoring files without metadata or with invalid/zeroed values, and skips files that already follow the target naming scheme. - Automatic collision handling: If another file already has the same timestamp within the same second (such as burst shots), it appends a sequential suffix (
20200612_205732_1.jpg,_2.jpg, and so on) and usesmv -nto avoid overwriting existing files. - Safe preview mode (
-d/--dry-run): Shows what would be renamed without modifying any files on disk. - Italian or English messages (
-l IT|EN): Detects the system locale (LC_ALL,LC_MESSAGES,LANG) and lets you force the language for both--helpand status/error messages.
Installation
The only external dependency required is ExifTool:
- Debian / Ubuntu / derivatives:Bash
sudo apt install libimage-exiftool-perl - macOS (Homebrew):Bash
brew install exiftool
Save the script—for instance at ~/.local/bin/exif-rename—and make it executable:
chmod +x ~/.local/bin/exif-rename
The Complete Script
#!/usr/bin/env bash
set -u
usage() {
if [ "$MSG_LANG" = "it" ]; then
cat <<EOF
Uso: $0 [OPZIONI] [PERCORSO...]
Rinomina foto e video in base alla data di scatto (metadati EXIF).
Formato: AAAAMMGG_HHMMSS.ext (es. 20200612_205732.jpg).
I file senza metadati utili vengono saltati.
Opzioni:
-r, --recursive cerca ricorsivamente nelle directory
-d, --dry-run mostra cosa verrebbe fatto senza rinominare
-l, --lang IT|EN scegli la lingua dei messaggi (italiano o inglese); di default usa la lingua di sistema
-h, --help mostra questo aiuto
Se non indichi percorsi, usa la directory corrente.
EOF
else
cat <<EOF
Usage: $0 [OPTIONS] [PATH...]
Rename photo and video files according to capture date (EXIF).
Format: YYYYMMDD_HHMMSS.ext (e.g., 20200612_205732.jpg).
Files without useful metadata are skipped.
Options:
-r, --recursive recurse into subdirectories
-d, --dry-run show what would be renamed without actually renaming
-l, --lang IT|EN choose language for messages (Italian or English); defaults to system locale
-h, --help display this help
If no paths given, current directory is used.
EOF
fi
}
lang="${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}"
case "$lang" in
it*|IT*) MSG_LANG=it ;;
en*|EN*) MSG_LANG=en ;;
*) MSG_LANG=auto ;;
esac
help_requested=0
recursive=0
dry_run=0
lang_opt=""
paths=()
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) help_requested=1; shift ;;
-r|--recursive) recursive=1; shift ;;
-d|--dry-run) dry_run=1; shift ;;
-l|--lang)
if [ $# -lt 2 ]; then
echo "error: -l requires an argument (IT or EN)" >&2
exit 2
fi
lang_opt="$2"; shift 2
;;
--) shift; paths=("$@"); break ;;
-*) echo "error: unknown option: $1" >&2; exit 2 ;;
*) paths+=("$1"); shift ;;
esac
done
if [ -n "$lang_opt" ]; then
case "$lang_opt" in
it|IT) MSG_LANG=it ;;
en|EN) MSG_LANG=en ;;
*) echo "error: invalid language: $lang_opt (use IT or EN)" >&2; exit 2 ;;
esac
fi
if [ "$MSG_LANG" = "it" ]; then
MSG_EXIFTOOL="errore: exiftool non trovato. Installalo (es. sudo apt install libimage-exiftool-perl)"
MSG_SKIP_NO_META="saltato (nessun metadato)"
MSG_RENAMED="rinominato"
MSG_WOULD_RENAME="rinominerei"
else
MSG_EXIFTOOL="error: exiftool not found. Install it (e.g. sudo apt install libimage-exiftool-perl)"
MSG_SKIP_NO_META="skipped (no metadata)"
MSG_RENAMED="renamed"
MSG_WOULD_RENAME="would rename"
fi
if [ "${help_requested:-0}" -eq 1 ]; then
usage
exit 0
fi
if ! command -v exiftool >/dev/null 2>&1; then
echo "$MSG_EXIFTOOL" >&2
exit 1
fi
find_depth=()
if [ "$recursive" -eq 0 ]; then
find_depth=(-maxdepth 1)
fi
search_roots=(".")
if [ ${#paths[@]} -gt 0 ]; then
search_roots=("${paths[@]}")
fi
files=()
for p in "${search_roots[@]}"; do
while IFS= read -r -d '' f; do
files+=("$f")
done < <(find "$p" "${find_depth[@]}" -type f ! -name '.*' -print0)
done
for f in "${files[@]}"; do
base=$(basename -- "$f")
case "$base" in
*.*) ext="${base##*.}" ;;
*) continue ;;
esac
dt=$(exiftool -d "%Y%m%d_%H%M%S" -s3 -DateTimeOriginal -CreateDate -MediaCreateDate "$f" 2>/dev/null | head -n1) || true
# Validate date: must be YYYYMMDD_HHMMSS (8 digits + _ + 6 digits)
if [[ ! "$dt" =~ ^[0-9]{8}_[0-9]{6}$ ]]; then
echo "$MSG_SKIP_NO_META: $f" >&2
continue
fi
# Skip if already correctly named
if [ "$base" = "${dt}.${ext}" ]; then
continue
fi
dir=$(dirname -- "$f")
new="$dir/${dt}.${ext}"
if [ -e "$new" ]; then
j=1
while [ -e "$dir/${dt}_${j}.${ext}" ]; do
j=$((j + 1))
done
new="$dir/${dt}_${j}.${ext}"
fi
if [ "$dry_run" -eq 1 ]; then
echo "$MSG_WOULD_RENAME: $f -> $new"
else
mv -n -- "$f" "$new"
echo "$MSG_RENAMED: $f -> $new"
fi
done
Usage Examples
Preview what would be renamed in the current directory without making any changes:
exif-rename --dry-run
Recursively rename files across one or more paths:
exif-rename -r ~/Pictures/Archive ~/Backups/Phone
Show help in English or force messages into a specific language:
exif-rename -l EN --help
exif-rename -l IT -d ./photos