Separating iPhone photos and videos by date on Linux


Posted by Diego Assencio on 2016.11.25 under Linux (Shell)

Unfortunately, iPhones (and iPads) do not offer the possibility of automatically grouping photos and videos by date on separate directories. If you prefer organizing your photos and videos that way, you can transfer them to your computer and then run the script below at the directory to which there were transferred; it will automatically determine the dates at which your photos and videos were created and place them in directories with names in the format YYYY-MM-DD (e.g. all photos and videos created on the 15th of January of 2016 will be placed under the directory 2016-01-15).

Before you can run the script, you need to install the mediainfo and exiv2 packages. For that, open a terminal and run:

sudo apt-get install exiv2 mediainfo

The script below can also be downloaded directly by clicking here.

#!/bin/bash

# make the script work even if the photos/videos have been renamed
IFS=$'\n'

# process the photos first
for file in $(ls *.JPG 2>/dev/null)
do
    # determine the date at which the photo was taken
    date=$(exiv2 $file 2>/dev/null | \
           grep timestamp | \
           cut -d ' ' -f 4 | \
           sed 's/:/-/g')

    if [ ! -z "$date" ]
    then
        mkdir -p $date
        mv $file $date
    else
        echo "Ignoring photo '$file' (no valid date found)"
    fi
done

# now process the videos
for file in $(ls *.MOV 2>/dev/null)
do
    # determine the date at which the video was recorded
    date=$(mediainfo --fullscan $file 2>/dev/null | \
           grep com.apple.quicktime.creationdate | \
           grep -Eo '[0-9]{4}-[0-9]{2}-[0-9]{2}')

    if [ ! -z "$date" ]
    then
        mkdir -p $date
        mv $file $date
    else
        echo "Ignoring video '$file' (no valid date found)"
    fi
done

Comments

No comments posted yet.

Leave a reply

NOTE: A name and a comment (max. 1024 characters) must be provided; all other fields are optional. Equations will be processed if surrounded with dollar signs (as in LaTeX). You can post up to 5 comments per day.