Updating File Create Date From Exif Data

When exporting photos from a service like Flickr, perhaps after they've given notice that they're going to delete our photos if you don't subscribe to their paid plan, you might run into an issue where the photos you've downloaded have incorrect created and modified dates.

To fix this, we need to build a quick bash script to update all the created and modified dates from the photo's EXIF data.

We're going to be using two built in Mac OSX command line tools to do this...

Lists the metadata attributes for the specified file (including EXIF data)
Sets attributes of files and directories

First, fire up your editor of choice again and create a new file named update_file_dates.sh

Paste in the following bash script code...

for f in *.jpg; do
  echo "Processing $f";
  created_date_from_exif=$(mdls -raw -n kMDItemContentCreationDate "$f") # Pulls creation date from image's exif data
  formated_created_date=$(date -f '%F %T %z' -j "$created_date_from_exif" '+%D %T %z') # Converts creation date to a format that can be used by the date command
  SetFile -d "$formated_created_date" "$f" # Sets the file's creation date to the date pulled from the exif data
  SetFile -m "$formated_created_date" "$f" # Sets the file's modification date to the date pulled from the exif data

Then mark the file as executable...

> chmod +x update_file_dates.sh

Then execute it...

> ./update_file_dates.sh

The script will loop over every jpg file in the current directory and update the file's creation and modification dates with the date found in the images's EXIF data.

Otherwise you can also use the excellent exiftool application developed by Phil Harvey...

> exiftool "-FileModifyDate<DateTimeOriginal" *.jpg


Post A Twitter Response To This Blog Post

To: @mattccrampton