Organize your pictures by EXIF tags with bash

Just a simple script to order your thousands of pictures by putting them in subdirectories on the basis of EXIF shot date:

#!/bin/bash
# BashButler 1.0 - 11/2007 by Eugenio Rustico
# Bash script to organize your pictures in folders by date
#
# GPL v3 license http://www.gnu.org/licenses/gpl-3.0.html

ls -1 *.jpg | while read fn
do
export dt=`exiv2 "$fn" | grep timestamp | awk '{ print $4 }' | tr ":" "-"`
if test ! -e ./$dt
then
mkdir $dt
fi
mv "$fn" ./$dt
echo File "$fn" moved in "$dt"
done

echo Good job!

I wrote it to setup 4.900 pictures which were all in the same directory, and it was really fast and useful. My unstoppable sense of humor conceived this amazing script name: Bash Butler. I'm very sorry for this. =)

Notes:
  • Of course you need exiv2 package on your system, but you can use other cli exif tools by modifying only one row;
  • With very little changes, you can modify the script to organize your pictures by different EXIF tags (e.g. different folders for different digital cameras);
  • Filenames with spaces are treated correctly;
  • By default mv does'nt overwrite destination files: so all remaining files in current directory are copies, and can be deleted.
GPL license v.3, of course.

6 comments:

Lazy Googler said...

Thanks, just what I was looking for... so simple.

Anonymous said...

Really many thanks. This is exactly what I was looking for.

schickb said...

Here is an enhanced version: organize

Kaan said...

#!/bin/bash
# PictureRenamer 1.0 - 08/2009 by Kaan Atasever
# This script renames your pictures with date and time
#
# GPL v3 license http://www.gnu.org/licenses/gpl-3.0.html

ls -1 *.jpg | while read fn
do
export pa1=`exiv2 "$fn" | grep timestamp | awk '{ print $4 }' | tr ":" "-"`
export pa2=`exiv2 "$fn" | grep timestamp | awk '{ print $5 }' | tr ":" "-"`
export pa="$pa1 $pa2.jpg"
mv "$fn" "$pa"
echo File "$fn" renamed as "$pa"
done

echo All done!

Ruben said...

Beautiful and simple script. Thanks.

I searched around to extract the info from the 3gp video files done by some phones, but got inconsistent results.

I finally get the date from the creation time of the file (correct me if I am wrong).

Here is my modification of your script for THOSE 3GP FILES.


#!/bin/bash
# BashButler 1.0 - 11/2007 by Eugenio Rustico
# Modified by Ruben
# Bash script to organize your 3gp files in folders by date
#
# GPL v3 license http://www.gnu.org/licenses/gpl-3.0.html

ls -1 *.3gp | while read fn
do
#export dt=`exiv2 "$fn" | grep timestamp | awk '{ print $4 }' | tr ":" "-"`
export dt=` stat -c %z "$fn" | awk '{ print $1 }' `
if test ! -e ./$dt
then
mkdir $dt
fi
mv -i "$fn" ./$dt
echo File "$fn" moved in "$dt"
done

echo "Good job"

Andy said...

Be careful if you have pics taken within same second - the script as it is now will overwrite all but the last one.

Post a Comment