Skip Navigation

Shell Snippet to Fix File Dates

When I advanced the hour on our camera for daylight-savings time, I inadvertently changed the year back to 2005. About six weeks of pictures were affected. No biggie, but it bugged me, so here's the shell script snippet I used to fix it. I used stat to get the month, day, and time of the files and then touch to reset the date. Note that this does not change the date in the EXIF data, which is still wrong.

#!/bin/sh

for f in *.[jJ][pP]*[gG]
do
	if [ -e "$f" ] || continue
	then
	echo "$f"
	# show the date before the change
	stat -f "%Sm" -t "%m/%d/%G %H:%M:%S" "$f"
	# change the date
	touch -t 2006`stat -f "%Sm" -t "%m%d%H%M.%S" "$f"` "$f"
	# show the date after the change
	stat -f "%Sm" -t "%m/%d/%G %H:%M:%S" "$f"
	else
	echo "No JPEGs in `pwd`"
	fi
done

It’s odd that I am blessed with the ability to fix this kind of problem, but cursed with the stupidity that caused it.