Sort images by year, month, day, hour, minute, second

Started by Mike0, April 30, 2013, 12:02:17 PM

Previous topic - Next topic

Mike0

So I need some help with a bash script for sorting images by date.

The script takes two arguments (Source and Destination), then sorts the images using DateTimeOriginal into folders in the following order:

/Destination/YYYY/MM/MMDD-HHMMSS.jpg

So 2013/04/0430-133022.jpg would be an example of a picture.

If we use only one argument, it starts to search for files in the current directory, if we use 0 arguments, it throws error to stderr and echo's : usage : programName.sh [SOURCE] DEST

and throws exit status = 1

Before we start making folders, we need to check if we can write into the DEST folder.

We mustnt move or copy photos, but just make a link to the new photos.

If the program is succesful, it has to throw exit code 0 at the end.

I need this until 6th of may 2013, thank you for any help  :-\

In real time the program has to echo the current folder we are working on and add a dot (.) for each photo that is in it.

For example:

$./urediFoto.sh
Usage: urediFoto.sh [SOURCE] DEST

$./urediFoto.sh Desktop Pictures
Desktop/card/DCIM/111_PANA/..
Desktop/card/DCIM/110_PANA/.
Desktop/slike/..
Desktop/DCIM/100NCD80/...
Desktop/sneg/.

Before



After



The pictures that have to be sorted are here:

http://www.fileconvoy.com/dfl.php?id=g618b83a9b1ce2762999277723512d5b1df82f980a


So far I have this code:

#!/bin/bash

SOURCE="$1"
DEST="$2"

for f in $(find $1 -iname "*.jpg" -iname "*.JPG")

do
       
        if [ -f $f ]; then
               
timestamp="$(identify -format '%[exif:DateTimeOriginal]' $f)"
               
                y=$(echo $timestamp | cut -c 1-4)
                m=$(echo $timestamp | cut -c 6-7)
                d=$(echo $timestamp | cut -c 9-10)

ura=$(echo $timestamp | cut -c 12-13)
min=$(echo $timestamp | cut -c 15-16)
sec=$(echo $timestamp | cut -c 18-19)

                destFile=$2/$y/$m/$d/$(basename $f)

                        mkdir "$2/$y/$m" -p


         fi

                        cp $f $destFile
                        echo "Moved $f to $destFile"
done[code]


Mike0

I have a newer version of the code, but find and timestamp still dont work. How do I read DateTimeOriginal, and how do I properly use find to find .jpg and .JPG ?


#!/bin/bash

SOURCE="$1"
DEST="$2"

if [ $# -eq 0 ]

then

    echo "Uporaba: $0 [SOURCE] DEST"
exit 1

else [ $# -eq 1 ]

SOURCE=$(pwd)

DEST="$1"



fi


for f in $(find $SOURCE -iname "*.jpg" -iname "*.JPG")

do
       
timestamp=$(exiftool -TAG "-DateTimeOriginal")
               
                y=$(echo $timestamp | cut -c 1-4)
                m=$(echo $timestamp | cut -c 6-7)
                d=$(echo $timestamp | cut -c 9-10)

ura=$(echo $timestamp | cut -c 12-13)
min=$(echo $timestamp | cut -c 15-16)
sec=$(echo $timestamp | cut -c 18-19)



                        mkdir -p $DEST/$y/$m/

ln -s -f $f "$DEST/$y/$m/$m$d-$ura$min$sec.jpg"

done

exit 0[code]