Skip to content

Instantly share code, notes, and snippets.

@unabridgedxcrpt
Last active December 15, 2015 21:05
Show Gist options
  • Save unabridgedxcrpt/a5e699a1564da6d05db0 to your computer and use it in GitHub Desktop.
Save unabridgedxcrpt/a5e699a1564da6d05db0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# DESCRIPTION: Take filename in format "ccyy-mm-dd hh-mm-ss*" and use this to populate the exif Date/Time Original field
# USAGE: ./filedate2exiforiginal.sh file1, file2, etc. Can use bash file globbing
# FUTURE IMPROVEMENTS: 1. check if date-time-original field already populated and optionally skip or overwrite
# 2. sed command can be much more elegant have used the hammer method here :)
if [ $# -eq 0 ]
then
cat << _EOF_
USAGE: $0 file1 file2 ..., or
$0 *.jpg, or
$0 dir/*.jpg
...
_EOF_
exit
fi
while [ "$1" != "" ]; do
# Skip directories
if [ -d "$1" ]; then
shift
continue
fi
#Improvement here: if exif:datetimeoriginal is already set, ask if want to change it?
VERSION="${1}" #get filename
# echo "-------------" + $VERSION
VERSIONTIME="${VERSION//[!0-9]/}" # remove non-digit characters
# echo $VERSIONTIME
VERSIONTOUCH="${VERSIONTIME:0:14}" # return only the first 14 digits
# echo $VERSIONTOUCH
VERSIONTOUCHSED=`echo $VERSIONTOUCH | sed 's/^\(.\{4\}\)/\1:/'`
VERSIONTOUCHSED1=`echo $VERSIONTOUCHSED | sed 's/^\(.\{7\}\)/\1:/'`
VERSIONTOUCHSED2=`echo $VERSIONTOUCHSED1 | sed 's/^\(.\{10\}\)/\1 /'`
VERSIONTOUCHSED3=`echo $VERSIONTOUCHSED2 | sed 's/^\(.\{13\}\)/\1:/'`
VERSIONTOUCHSED4=`echo $VERSIONTOUCHSED3 | sed 's/^\(.\{16\}\)/\1:/'`
# so we can use exiftool with proper format for date "ccyy:mm:dd hh:mm:ss"
# echo "VERSIONTOUCHSED4" "${VERSIONTOUCHSED4}"
exiftool -common "${1}"
exiftool -DateTimeOriginal="${VERSIONTOUCHSED4}" "${1}"
exiftool -common "${1}"
shift
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment