Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Organize a huge number of photos into a hierarchy UNIX
I often face the problem of sorting through thousands of pictures and wanting to create some sensible hierarchy before importing them into another program or burning them to a DVD. This tip will move pictures into a year/month directory hierarchy naming each file based on the day and time it was taken. This usually corresponds well to subject or location. THIS HINT WILL MOVE AND RENAME YOUR FILES. DO NOT RUN IT ON YOUR ONLY COPY!

Read the rest of the hint for the how-to...

[robg adds: Heed the warnings; do not do this to your only copy of a given set of images! Personally, I just let iPhoto manage everything, but then again, I don't take and manage images for a living...]

First make a hierarchy of directories:
% mkdir your_output_directory
% cd your_output_directory
% for y in 2000 2001 2002 2003 2004 ; do 
> mkdir $y ; cd $y ; 
> mkdir 01_Jannuary 02_February 03_March 04_April ; 
> mkdir 05_May 06_June 07_July 08_August ;
> mkdir 09_September 10_October 11_November 12_December ; 
> cd .. ; 
> done
[Type what's shown on each line, but without the leading % or >]

I chose to put the leading numbers on the month names so when viewing sorted by name, they come out in calendar order. If you change this, you will need to change the jhead command lines below! Did I mention that this hint will move and rename your files -- do not run it on your only copy!. I guess that's twice. I should also mention do not run it on files that are linked to an application!. When doing dangerous things to thousands of pictures, it's worth the time to make a second copy and work from that.

Next, get or have a copy of jhead, and RTFM (read the fine manual). Go for it:
jhead -n'/full/path/to/your_output_directory/%Y/%m_%B/%d-%T' source_dir/*
Since you did read the fine manual, you know all about -exonly and -model, as well as the difference between the -n and -nf options. I, myself, always do a -ft as well. If you read the copy on the web, you noticed that jhead's recursive feature is only supported on Windows, so I offer the following workaround:
find source_dir -type f -name '*.jpg' \
 -exec jhead -n'/full/path/to/your_output_directory/%Y/%m_%B/%d-%T' {} ;
When you're done, you'll have your photos in a nice hierarchy something like this:
./2001/05_May/12-09:44:57.jpg
./2003/05_May/03-14:41:19.jpg
./2003/06_June/13-11:16:00.jpg
./2003/06_June/13-11:16:06.jpg
./2003/06_June/28-15:56:19.jpg
The filenames may initially be hard to read, but they make sense. In the above example, for 2003 we have one photo from May 3 and then two photos from the 13th of June -- the first one taken at 11:16:00 (h:m:s) and the other taken six seconds later. Then finally one on the 28th of June. The leading digits on the month names has the nice benefit that you can get a text listing in chronological order with:
find . -type f -print | sort
You can also get a histogram by hour:
find . -type f -print | cut -d- -f2 | cut -d: -f1 | sort | uniq -c | sort +0nr
Or by year/month:
find . -type f -print | cut -d/ -f1-3 | sort | uniq -c | sort +0nr
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,856 views]  

Organize a huge number of photos into a hierarchy | 4 comments | Create New Account
Click here to return to the 'Organize a huge number of photos into a hierarchy' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
What about DateTree?
Authored by: qryss on Jun 24, '04 03:39:11AM

I use this lovely little program to organise my photos. And you have control over how the date hierarchy is built.

http://www.orange-carb.org/DateTree/

qryss



[ Reply to This | # ]
What about DateTree?
Authored by: MartySells on Jun 25, '04 07:30:25PM
BETTER!

I submitted the original hint. DateTree appears to be a very nice GUI app which does essentially the same thing. Unless you want to script this sort of thing from the unix side or are more comfortable with a shell prompt I'd suggest you use DateTree.

The only difference I see is that DateTree doesn't name files based on the modification time. I'll email the author and suggest this to him.

$7 is an amazing deal to support the DateTree developer and he's even from where I went to high school!

-m

[ Reply to This | # ]
Organize a huge number of photos into a hierarchy
Authored by: rotaiv on Jan 06, '05 04:59:54PM
I liked the idea of using jhead but my needs were a little different. I wanted my target directory structure like this: /Photos/2004/01/06. Also, I did not want to change the image name. Finally, I wanted to use jhead's "-ft" to change the file time to the EXIF capture date/time.

I got the idea after looking at DateTree. This is a really nice application and will do almost what I wanted. The only difference is that I wanted the files "moved" and not "copied".

Since the files are not renamed, there is the possibility of a duplicate so I had to factor that in as well. I've written a few perl scripts before so I decided to write one to satsify my needs. It's fairly basic but does the job :)


#!/usr/bin/perl

use File::Path;
use File::Basename;

$basedir = "~/Photos";

# Process each file
foreach $fpath (@ARGV) { &main; }

sub main {

  # Skip invalid files
  if(! -e $fpath) {
    print "$fpath -> [invalid]\n";
    next;
  }

  # Read EXIF data from image
  @exifdata = `jhead -exonly $fpath`;

  # Skip if no EXIF data found
  if ($#exifdata == 0) {
    print "$fpath -> [skipped]\n";
    next;
  }

  # Process each EXIF field
  foreach (@exifdata) {

    # Look for "Date/Time" field
    if ( ($imgyr, $imgmo, $imgdy) = /Date\/Time.*\s:.(\d+):(\d+):(\d+)\s./ ) {

      $imgdir="$imgyr/$imgmo/$imgdy";

      $fname = File::Basename::basename($fpath);

      # Check if duplicate file exist in target directory
      if(-e "$basedir/$imgdir/$fname") {
        print "$fpath -> [duplicate] $basedir/$imgdir/$fname\n";
        next;
      }

      # Make directories if not present
      mkpath("$basedir/$imgdir") unless -e "$basedir/$imgdir";
      # Change file date/time to match EXIF
      system("jhead -ft $fpath > /dev/null");
      print "$fpath -> $basedir/$imgdir/$fname\n";
      # Move file to target directory
      rename($fpath,"$basedir/$imgdir/$fname");

    }

  }

}
As the original post suggested, you can use "find" to search for the files you need and execute this script:

find ~/NewPhotos -type f -name *.JPG -exec mvimg.pl {} \; | tee mvimg.log
The "| tee mvimg.log" at the end creates a log file of the output so you can grep for "skipped" or "duplicate" messages.

I'll probably just use it like mvimg.pl *.JPG from the directory containing the photos I want to organize.

Don't forget to put this script in your path somewhere and chmod u+x mvimg.pl so it is executable.

[ Reply to This | # ]

Organize a huge number of photos into a hierarchy
Authored by: rotaiv on Jan 06, '05 05:02:10PM

I forgot to mention that I used to use iPhoto but for various reasons, I switched to iView Media. My intention now is to download my new images, run my <tt>mvimg.pl</tt> script then add them to my iView Media catalogs.



[ Reply to This | # ]