Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Last active September 28, 2018 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirafrank/9970c15fec0fa6e49be5363ef6ed1cf3 to your computer and use it in GitHub Desktop.
Save pirafrank/9970c15fec0fa6e49be5363ef6ed1cf3 to your computer and use it in GitHub Desktop.
Update EXIF datetime data from filename pattern (if not set) or update file timestamps from EXIF metadata (if it is set)
#!/bin/bash
# Simple script to parse JPG photo filename and set exif datetime,
# creation date and file modification date accordingly.
# Filename pattern is YYYYMMDD_hhmmss.
# Timezone is taken by your PC.
# exec this script by searching for .JPG and .jpg files in given folder
# find . -type f -name "*.JPG" -o -name "*.jpg" -exec /path/to/exif_datetime_setter.sh {} \;
if [ -z "$1" ]; then
echo "Error: no input file as argument. Exiting..."
exit 1
fi
filepath="$1"
echo "Working on: $filepath ..."
filename="${1##*/}"
year=$(echo "$filename" | cut -c1-4)
month=$(echo "$filename" | cut -c5-6)
day=$(echo "$filename" | cut -c7-8)
hour=$(echo "$filename" | cut -c10-11)
mins=$(echo "$filename" | cut -c12-13)
secs=$(echo "$filename" | cut -c14-15)
arg2="$year:$month:$day $hour:$mins:$secs"
echo "Parsed data: $arg2"
# from exiftool docs:
# "If the Geotime tag is not specified, the value of DateTimeOriginal
# is used for geotagging. Local system time is assumed unless
# DateTimeOriginal contains a timezone."
# if datetimeoriginal is set, set file modification and file creation dates to datetimeoriginal value
#exiftool -if 'defined $datetimeoriginal and not $datetimeoriginal =~ /(^\s*$)/' \
exiftool -if 'defined $datetimeoriginal and not $datetimeoriginal =~ /(^\s*$)/' \
"-FileCreateDate<DateTimeOriginal" "-FileModifyDate<DateTimeOriginal" "$filepath"
# if datetimeoriginal is missing, is empty or contains only spaces, set datetimeoriginal from data parsed from filename
exiftool -if 'not defined $datetimeoriginal or $datetimeoriginal =~ /(^\s*$)/' \
-overwrite_original -DateTimeOriginal="$arg2" -filemodifydate="$arg2" \
-FileCreateDate="$arg2" -createdate="$arg2" -modifydate="$arg2" "$filepath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment