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.


# 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
    datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2 }' | sed s%:%/%g)"
    if ! test -e "$datepath"; then
        mkdir -pv "$datepath"

    mv -v $fil $datepath



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

a little java tool

drseergio's picture

Hey, I've built a small Java tool which some of you might like:

It's far from complete but it already is working. ima-so uses XML configuration file to specify the target folder structure, however, at the moment there is no UI dialog to configure it. If you are curious, you can access the xml from within the jar file.

rename to date

Jay Zach's picture

I wrote a script several years ago that uses exif or exiftags to automatically rename all jpg's within a directory by the date held within the exif information. I never have any duplicate filenames that way, and they're easy to sort (I do it by year, but month wouldn't be a problem either).

Another plug for exiftool

mfk's picture

Exiftool is definitely the way to go. Here is the gist of a command I use to accomplish the same task:

for file
    exiftool -d "${BASE}/%Y/%m/%d" "-directory<createdate" $file

Exiftool takes care of creating all of the intermediate directories. It can also rename the files if you so desire:

for file
    exiftool -d "${BASE}/%Y/%m/%d/img_%Y%m%d_%H%M%S%%-c.%%e" "-filename<createdate" $file

The '%%-c' format spec increments a counter to handle the case of multiple photos taken during the same second and '%%e' keeps the same filename extension as the original.

This program is the "swiss army knife" of photo metadata manipulation.

Please Help!

Jenny's picture

Um hi.

I've actually been wanting to use Linux Ubuntu, but I've been having trouble and don't really have anyone to help me.

This guy at school told me to use Linux Ubuntu and he said it's a really good program and gave me a CD that says "Ubuntu Warty". He said its the same as using a Mac. I put it in and double click on it and can't get it to work right.

I also try to go to Start/Add new programs/ and still can't figure it out.

I get really tired of XP and everything and really want to try something new.

Sorry I don't know much about computers.

Hi! Do not use the Warty, it

Anonymous's picture

Do not use the Warty, it is very, very old one! It is the first version of Ubuntu, and has no more updates since april 2006. Try to download the latest one (Ubuntu 9.04 Jaunty Jackalope), or wait just for the upcoming Ubuntu 9.10 Karmic Koala. It arrives on 29th of october, 2009. Burn it on a cd and use it as Richard110011 adviced, first as LiveCD, do not install it. Think about if you will have any problem with Ubuntu but you ereased XP...

Installing Ubuntu

Richard110011's picture

Not sure what your problem is. Sounds like you are trying to install Ubuntu from XP. With all the Linux versions I've used, you have to insert the CD/DVD in your drive and reboot your PC. Ubuntu will then boot from the CD/DVD and install itself on your machine's hard drive.

Note: to install Ubuntu you will need to partition your drive. Keep the existing partition for XP, and create a new one for Ubuntu. You need to be careful, though. Don't overwrite your XP partition or any other existing partition - create a separate one for Ubuntu! Hopefully, Ubuntu will do all this for you, though.

Once installed, your PC will boot to a menu, allowing you to select XP or Ubuntu.

Linux is a hell of a lot better than it used to be. Unfortunately, it's not like using a Mac. Lack of manufacture support is partly to blame.

I think you should install from a version that doesn't make any changes to your PC:

Scroll down to "Live CD".

WartyWarthog was the first release of Ubuntu:

You can download the latest version here:

Unfortunately, uninstalling Linux might give you serious problems - you might even lose XP! - if you know so little about computers. So, try to find a version that doesn't permanently install itself on your PC - such versions are known as "Live CD".

Ubuntu explains how to use its CD as a "Live CD" - that is, to boot without making any changes to your PC:

You choose the top option: "Try Ubuntu without any change to your computer".

Thanks a lot. Very helpful

Apopas's picture

Thanks a lot. Very helpful scripts all of them.

One contribution.

Nacho Lamas's picture

Excellent post!

One contribution:

for sorting files other than images (vids?), or for people not having ImageMagick available, just substituting
for fil in *.jpg
for fil in *
datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2 }' | sed s%:%/%g)"
datepath="$(stat -c '%y' $fil | awk '{print $1 }' | sed s%-%/%g)"

might do the trick (though sorting by modification date, not original creation date, of course).


Anonymous's picture

i suggest using exiftool by Phil Harvey. It extracts the exif information a lot faster than imagemagick's identify.

Instead of

datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2 }' | sed s%:%/%g)"

you can do

datepath="$(exiftool -CreateDate IMG_2299.JPG|awk '{print $4}'|sed s%:%/%g)"


Incrementing under subject matter?

Anonymous's picture

Here's my dilemma:

I have a lot of photosets that I like to put under subject matter and increment the numbers, and then be able to add new photos starting from the last number.

Let me explain. Think "Christmas 2008" photos. I'd like to dump a whole set of these photos from my digital camera into a folder so each photo itself is called e.g. "Xmas2008-01.jpg" "Xmas2008-02.jpg" "Xmas2008-03.jpg" etc - you get the idea.

Assume I end at Xmas2008-45.jpg. And then, when my sister and brother send me their photos that they took at the same event, I want to be able to easily rename theirs starting incrementally, e.g. my sister's photos added to that folder might be "Xmas2008-46.jpg" through "Xmas2008-58.jpg" and my brother's might be "Xmas2008-59.jpg" "Xmas2008-71.jpg"

Any thoughts on how to do something like that?

I'd suggest an even better

Anonymous's picture

I'd suggest an even better way than using sequential numbers (since your sister's photos will be out of order with yours) is to use exiftool to apply a HHMMSS timestamp as a number, then you can get photo set from multiple people and (as long as photos weren't taken by two different people at the event at exactly the same second) it'll merge the results and you'll always view the photos in the order they were taken.

Re: Incrementing under subject matter?

sgcg's picture

It is easy to do it with a shell script. You can copy the files to your destination directory, "Christmas 2008", with the following script:

if [ -d "$DESTINATION" ]; then
  last_picture=$(ls "$DESTINATION" | sort | tail -n1)
  counter=$(echo "$last_picture" | sed 's/.*-\([0-9]\+\)\.jpg$/\1/')
  mkdir "$DESTINATION"
ls "$ORIGIN" | sort | while read file; do
  destfile=$(printf '%s/%s-%02d.jpg' "$DESTINATION" "$PREFIX" $counter)
  cp "$origfile" "$destfile"

Suppose that your files are at the directories "my-pictures" "sister-pictures" and "brother-pictures", you want to put all the pictures at the directory "Christmas 2008" and the files will be named "Xmas2008-01.jpg", etc. The script is called "copy-pictures" and is, for example, in your current directory. You can do:

./copy-pictures my-pictures 'Christmas 2008' Xmas2008
./copy-pictures sister-pictures 'Christmas 2008' Xmas2008
./copy-pictures brother-pictures 'Christmas 2008' Xmas2008

I use Xfce and it's file

jspence's picture

I use Xfce and it's file manager is thunar. It has a built-in command for bulk renaming. There are several options available and one of them is start number. Also, gThumb Image Viewer will allow you to rename files, in bulk, and select a start number. I would think that several other of the photo viewing programs would have this option. These are the only ones that I am aware of because that is all I use.


beware of doing this with raw images...

Kyle's picture

Beware of using this script with raw images: identify will take a long long time because it wants to decompress each raw image (my experience here is only with Nikon NEFs, but I assume Canon raw files are similar beasts). Since my wife and I always shoot in raw, we have to use exiftool to extract the data instead.

A fun trick with identify in scripts like this is to grep for "Corrupt JPEG data": it's sad to find a corrupt image, but at least you know to grab it off of your camera again, or scour your drive for a good copy of it!

Thanks Kyle, for your

Baliinc's picture

Thanks Kyle, for your awareness

how to change the directory format?

Anonymous's picture

Can the directory format be easily changed to 2009-07-04 (such that there are not subdirectories for each month and day)?

Thanks for this script, it's almost exactly what I've been looking for. Canon's software does this automatically in Windows, but I haven't been able to find the Linux equivalent.

I adapted a script I found

Anonymous's picture

I adapted a script I found here:
to sort my pictures into directories. It was simple enough change the directory format to create directories the way I wanted them named.

To the person who wanted files renamed like Xmas2008-03.jpg, I would suggest using exiv2,
It seems easy enough to create a loop in a script and append 'Xmas' to the file it renames for you.

Running 'exiv2 mv *.jpg' in a directory will rename all the files into a date/time format filename, like 20081225100145.jpg. So unless you shot two pictures at the same second, you should have unique filenames, sequenced by time.


Mitch Frazier's picture

In this line:

  datepath="$(identify -verbose $fil | grep DateTimeOri | awk '{print $2 }' | sed s%:%/%g)"

Change the "/" (slash) to a "-" (dash) in the text after the sed expression:

    ... sed s%:%/%g

    ... sed s%:%-%g

Mitch Frazier is an Associate Editor for Linux Journal.

thank you

Anonymous's picture

Thanks for the speedy reply! Of course -- I didn't notice initially that the script scrolled to the right for the sed expression. Now it all makes much more sense.