exported iCloud photos have the wrong date
Scope: #
One of my friends has a problem:
they use iCloud photos to save/backup photos from the iPhone and want to create an out-of-iCloud backup of the photos on their mac.
The issue is that the photos are exported with consecutive names (es: IMG_1234.JPG) and the files have the creation/modification date of the moment you exported them from Photos to disk.
In this way the real date of the photo is only in Exif data, but we cannot see it directly or sort the photos using the file date.
My friend asked me to create a script to rename the photos with the correct capture date.
Prerequisites: #
- brew
- to install exiftool
- install it via: https://brew.sh/
- exiftool
- install it via brew:
- https://formulae.brew.sh/formula/exiftool
Script: #
#!/bin/bash
#check if the required tool is installed
if ! exiftool > /dev/null 2>&1 ; then
echo "exiftool is not installed, exiting"
exit
fi
#main cycle:
for file in "$@" ; do
#clean variables every cycle
filedate=""
folder=""
extensione=""
newname=""
#differentiate between files that need diferent fields in exiftool:
if [[ "$file" == *".MOV"* ]]; then
newname=$(exiftool -d "%Y-%m-%dT%H-%M-%S%z" -MediaCreateDate -S -s "$file")
else
newname=$(exiftool -d "%Y-%m-%dT%H-%M-%S%z" -DateTimeOriginal -S -s "$file")
fi
if [ ! -z "$newname" ] ; then
extension=$(echo ${file//*.})
#differentiate if file is in the same folder or another:
if [[ "$file" == *"/"* ]]; then
folder=$(echo "$file" | cut -d/ -f1)
mv "$file" "$folder/$newname.$extension"
#debug:
#echo "rebuilt: " "$folder/$newname.$extension"
else
mv "$file" "$newname.$extension"
#debug:
#echo "rebuilt: " "$newname.$extension"
fi
fi
#DEBUG:
#echo "newname: " "$newname"
#echo "file: " "$file"
#echo "extension: " "$extension"
#echo "folder: " "$folder"
#echo "- - - - - - - - - - "
done
Usage: #
just run the script against a single file or a folder:
#single file:
./rename.sh IMG_1234.JPG
#folder:
./rename.sh _WORKDIR/*
Unupported files: #
The script ignores filetypes that don't contain (interesting) exif data,
for example images downloaded from the web.
Those files will not be renamed and will retain their original filename.
Notes: #
different files, different exif data: #
I've noticed that .MOV files contain exif data but on another field,
other file types may behave differently.
ISO 8601: #
I've tried to follow ISO 8601 for the dates but MacOS doesn't like ":" in the filenames and in the GUI Finder automatically translates that in "/".
So our filenames will look like:
2022-06-02T13-04-45+0000
Instead of:
2022-06-02T13:04:45+0000
certified for: #
The script has been ran and tested on macOS Big Sur 11.6.6