Automatically sort pictures based on the date they were taken

I have been a linux user almost exclusively for at least 4 years, mainly using ubuntu. One of the things I particularly liked was the way the default tool for importing pictures (Shotwell) manages the files and organize them depending on the date they were taken.

Shotwell will create an arrange of folders starting by the year, then the month and then the individual days, then put the corresponding pictures inside. As much as I liked this system, I always found myself wanting to organize then in a different way, where I could quickly look at a wider range of images, for example by year, but still be able to tell from the date and a short description the contents without opening the folder.

My ideal organization is a folder with the year, then inside the pictures organized by day, but still in the folder name I would like the whole date before a short description I will add later. Therefore having for example 2016/2016-01-17_KA-Snow/.

Another reason why I decided to search for a script to organize my pictures is because Shotwell started creating many small thumbnail files of the raw files inside the pictures folders and they were really annoying me, as much as I created another script to eliminate those thumbnails from Shotwell. Also, the performance when loading pictures from the camera to import wasn't the best, most of the time taking a great amount of time just to display how many pictures were available for importing.

For the previous mentioned reasons, I searched online first and found this code, which does part of the job I wanted, then I modified it and here is the code I am using now every time I connect my camera and would like to import the pictures.

To notice is that I added command line options -s and -d for source and destination paths respectively, so that I could organize different folders I already have. And finally I also added the raw file extension of my camera ".RW2". I still need to include support for the video files, which I still need to copy by hand.

sort.sh

#!/bin/bash

# defaults
source=/media/luisardila/5FC6-EB2A/DCIM/
destination=/home/luisardila/Pictures/2016/

# Goes through all jpeg files in source directory, grabs date from each
# and sorts them into destination folder in directories according to the date
# additionally it copies as well raw files with the same name as the jpeg
# (C) Luis Ardila 2016 <luis.ardila@bozica.co>

# Getting options from the commad line
while getopts ":s:d:" opt; do
        case $opt in
        s)   # source
                source=$OPTARG
                ;;
        d)   #destination
                destination=$OPTARG
                ;;
        \?)
                echo "Invalid option: -"$OPTARG"" >&2
                exit
                ;;
        :)
                echo "Option -"$OPTARG" requires an argument" >&2
                exit
                ;;
        esac
done
i=0
# Executing move of files
for file in $source*.jpg $source**/*.jpg $source*.JPG $source**/*.JPG # CAPS
do
        echo $file
        if test -e "$file"; then
                datepath="$(identify -verbose "${file}" | grep DateTimeOri | awk '{print $2 }' | sed s%:%-%g)"
                if [[ "${datepath}" == "" ]]; then
                        year="$(identify -verbose "${file}" | grep date:modify | awk '{print $2 }' | awk -F '[-]' '{print $1}')"
                        datepath="${year}_Random"
                fi
                if ! test -e "${destination}${datepath}"; then
                        mkdir -pv "${destination}${datepath}"
                fi
                mv -v "${file}" "${destination}${datepath}"
                rawfile=$(echo ${file} | sed 's/.JPG/.RW2/g')
                if test -e "$rawfile"; then
                   mv -v "${rawfile}" "${destination}${datepath}"
                fi
        i=$(( $i + 1  ))
        fi
        echo "moved $i files"
done

If you would like also to eliminate those annoying Shotwell thumbnails, here is what I do:

rmShotwell.sh

#!/bin/bash

find . -name "*shotwell*" -delete

Comments

Comments powered by Disqus