Last active
December 29, 2024 20:10
-
-
Save daniel-sc/566fcb3c97509ab544f9e9b08cc64e00 to your computer and use it in GitHub Desktop.
rename images (jpg, heic) to have filenames matching capturing date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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] *.[jJ][pP][eE][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