feat(images): rename photos using capture timestamps
feat(images): automate image renaming from EXIF metadata - Rename images using DateTimeOriginal EXIF timestamps - Fall back to file modification times when EXIF data is missing - Support JPG, JPEG, and PNG image formats - Generate consistent IMG_YYYYMMDD_HHMMSS filenames - Preserve original file extensions - Prevent filename collisions using numeric suffixes - Support processing arbitrary target directories - Add script documentation, requirements, and usage examples
This commit is contained in:
1 parent
804e17dbe6
commit
09cfddef94
2 files changed
+115
-44
No files matched your search
@@ -1,44 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Change to your target directory
|
||||
TARGET_DIR="./" # or set it via "$1" to pass folder as argument
|
||||
|
||||
if [[ "$1" != "" ]]; then
|
||||
TARGET_DIR="$1"
|
||||
|
||||
fi
|
||||
|
||||
cd "$TARGET_DIR" || exit 1
|
||||
|
||||
for file in *.{jpg,jpeg,JPG,JPEG,png,PNG}; do
|
||||
[ -f "$file" ] || continue # skip if not a file
|
||||
|
||||
# Extract DateTimeOriginal using exiftool
|
||||
datetime=$(exiftool -DateTimeOriginal -s3 "$file")
|
||||
|
||||
if [[ -n "$datetime" ]]; then
|
||||
# Format: "2023:08:21 14:32:10" -> "20230821_143210"
|
||||
formatted=$(echo "$datetime" | sed 's/://g' | sed 's/ /_/g' | cut -c1-15)
|
||||
echo $formatted
|
||||
else
|
||||
# Fall back to file modification time if no EXIF data
|
||||
formatted=$(date -r "$file" +"%Y%m%d_%H%M%S")
|
||||
fi
|
||||
|
||||
# Build new filename
|
||||
extension="${file##*.}"
|
||||
newname="IMG_${formatted}.${extension}"
|
||||
|
||||
# Avoid overwriting
|
||||
if [[ -e "$newname" ]]; then
|
||||
i=1
|
||||
while [[ -e "IMG_${formatted}_$i.${extension}" ]]; do
|
||||
((i++))
|
||||
done
|
||||
newname="IMG_${formatted}_$i.${extension}"
|
||||
fi
|
||||
|
||||
echo "Renaming: '$file' -> '$newname'"
|
||||
mv "$file" "$newname"
|
||||
done
|
||||
|
||||
Reference in new issue
Block a user