Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active October 2, 2018 04:53
Show Gist options
  • Select an option

  • Save lantrix/ffaa8acabb0e70f5cb167a471356d171 to your computer and use it in GitHub Desktop.

Select an option

Save lantrix/ffaa8acabb0e70f5cb167a471356d171 to your computer and use it in GitHub Desktop.
Rename dir of images to match their EXIF OriginalDate - also change Create Date and OS Modify date to match the EXIF OriginalDate
#!/bin/bash
# Processing all files in a directory, this script will:
# - Change Create Date of an image to match the EXIF OriginalDate
# - Change OS Modify date to match the EXIF OriginalDate of the image
# - Rename image to match EXIF OriginalDate in format:
# YYYY-MM-DD HH:mm:ss.ext
DIR="$1"
# failsafe - fall back to current directory
[ "$DIR" == "" ] && DIR="."
# save and change IFS
OLDIFS=$IFS
IFS=$'\n'
# read all file name into an array
fileArray=($(find $DIR -type f))
# restore it
IFS=$OLDIFS
# get length of an array
tLen=${#fileArray[@]}
echo "Number of files $tLen"
# use for loop read all filenames
for (( i=0; i<${tLen}; i++ ));
do
thisFile=${fileArray[$i]}
fileType=$(file -b -I ${fileArray[$i]})
if ! [[ $fileType == *"image"* ]]; then
echo "$thisFile is not an image ... skipping"
continue
fi
echo -n "$thisFile "
exiftool "${fileArray[$i]}" -ExifIFD:DateTimeOriginal
#change file Create Date
exiftool -overwrite_original '-CreateDate<DateTimeOriginal' "${fileArray[$i]}"
#change file OS modify date
exiftool -overwrite_original '-FileModifyDate<DateTimeOriginal' "${fileArray[$i]}"
#rename file
exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H.%M.%S%%-c.%%e" "${fileArray[$i]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment