Batch updating modification date to match EXIF

While importing some old photo’s from a backup of my old gallery into iPhoto, I found out this program uses file modification dates instead of EXIF for organizing my photo’s (boo, Apple!). Not wanting to spend hours scouring the web in search of some crappy application with a GUI that’s hard to understand, I whipped up this simple Shell script to do it for me:

#!/bin/sh
# Simple script to adjust file modification dates to EXIF dates
# Michel Jansen 2007
# http://micheljansen.org
# Licensed under CC Attribution 3.0 unporded
# http://creativecommons.org/licenses/by/3.0/

errorOccured=0;

if [ $# -eq 0 ]; then \
	echo;
	echo "  Usage:" `basename $0` "";
	echo "  For example:" `basename $0` "~/pictures/*.jpg";
	echo;
	errorOccured=1;
	exit 1;
fi

while [ -n "$1" ]; do
	date=`jhead "$1" | grep 'Date/Time' | sed -e "s/^[^:]*: *\([^:]*\):\([^:]*\):\([^ ]*\) \([^:]*\):\([^:]*\):\([^:]*\)/\1\2\3\4\5.\6/"` ;
	echo -n "Setting date of \"$1\" to $date..."; 
	if  `touch -cmt $date "$1"` ; then
		echo "OK";
	else
		echo "ERROR";
		errorOccured=1;
	fi
	shift;
done

exit $errorOccured;

It requires jhead and simply loops through all the files provided on the commandline, setting their last modified time to the one it reads from the picture’s EXIF data.

One Response to “Batch updating modification date to match EXIF”

  1. Michel says:

    Of course I only found out about jhead’s “-ft” option AFTER I wrote this script. Ah well, it may be useful for some purpose anyway :)

Leave a Reply

Your email address will not be published. Required fields are marked *