Skip to content

Instantly share code, notes, and snippets.

@simon-brooke
Created April 15, 2020 14:54
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 simon-brooke/d1ff616f0a36b3377143b560d47945db to your computer and use it in GitHub Desktop.
Save simon-brooke/d1ff616f0a36b3377143b560d47945db to your computer and use it in GitHub Desktop.
Set EXIF date of digital photographs to the current date
#!/bin/bash
# Set the timestamp of the JPEG files which are specified as arguments to the current
# date, if its timestamp is zero.
#
# Background: My Olympus E20p, which I like and still use, is so old that it has no
# clock, and doesn't timestamp photographs. In order that Shotwell should file them
# properly, I like them timestamped.
#
# Weirdly, although the timestamp appears in the EXIF as it comes off the camera as
# '0000:00:00 00:00:00'
#
# $ exiv2 -Y2020 P1011112.JPG
#
# produces the following:
#
# Image timestamp : 2019:11:30 00:00:00
#
# So it seems the zero date for EXIF timestamps is 30th November of the year 1 BC. BUT WHY?!?!
#
# As of today (20200415), using
#
# $ exiv2 -Y2020 -O4 -D16 filename
#
# produces the correct date but adds a mysterious extra hour; whether this is
# generalisable I don't yet know
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
DA=`echo "$DAY + 1" | bc`
USAGE="Usage: $0 filenames
Sets EXIF timestamp of each specified file to the current date, if it is unset"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
while (( "$#" ))
do
TIMESTAMP=`exiv2 $1 | grep timestamp`
if [ "$?" == "0" ]
then
if [ "${TIMESTAMP}" != "Image timestamp : 0000:00:00 00:00:00" ]
then
echo "$1 already has ${TIMESTAMP}: not changing"
else
exiv2 -Y${YEAR} -O${MONTH} -D${DA} $1
echo -n "$1: "
exiv2 $1 | grep timestamp
fi
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment