Skip to content

Instantly share code, notes, and snippets.

@daniel-sc
Created January 1, 2024 20:01
Show Gist options
  • Save daniel-sc/566fcb3c97509ab544f9e9b08cc64e00 to your computer and use it in GitHub Desktop.
Save daniel-sc/566fcb3c97509ab544f9e9b08cc64e00 to your computer and use it in GitHub Desktop.
rename images (jpg, heic) to have filenames matching capturing date
#!/bin/bash
# Use first argument as directory, default to current directory if not provided
IMAGE_DIR=${1:-$(pwd)}
# Change to the directory
cd "$IMAGE_DIR"
# Loop through HEIC, heic, JPG, and jpg files
for file in *.[hH][eE][iI][cC] *.[jJ][pP][gG]; do
# Check if the file is a regular file
if [ -f "$file" ]; then
# Extract the date from the metadata
dateTaken=$(exiftool -d "%Y%m%d_%H%M%S" -DateTimeOriginal "$file" | awk -F': ' '{print $2}')
# Check if the date was successfully extracted
if [ -z "$dateTaken" ]; then
echo "Error: Date could not be extracted from $file"
else
# Rename the file
mv "$file" "${dateTaken}_${file}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment