Tech Tip: Automaticaly Organize Your Photos by Date

Now that summer is over and your digital camera is full of pictures, how do you get them organized? At the command line of course! The script provided here automatically organizes them into sub-directories by date.

After my digital camera fills up with pictures, usually after a few weeks or months, I download them to my Ubuntu system. Usually they all end up in one directory. I find it more helpful to sort the image files by the date they were taken: most of the time I want to geotag them and usually photos that were taken on the same day are likely to have been taken in the same location also.

The following short script goes through the .jpg files in the current directory and gets the date stored within each image. It then creates a directory corresponding to the date (in case it doesn't exist) in the format year/month/day (all numeric) and copies the image into that directory. So for example, a photo called IMG_001.jpg taken on July 4th 2009 will end up under the path 2009/07/04/IMG_001.jpg.

The script requires the IMageMagick package, but that shouldn't be a problem on recent distributions.

#!/bin/sh

# Goes through all jpeg files in current directory, grabs date from each
# and sorts them into subdirectories according to the date
# Creates subdirectories corresponding to the dates as necessary.

for fil in *.jpg  # Also try *.JPG
do
    datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2 }' | sed s%:%/%g)"
    if ! test -e "$datepath"; then
        mkdir -pv "$datepath"
    fi

    mv -v $fil $datepath
done

Load Disqus comments