Organizing Files by Date Using Find

I had taken pictures using a friend’s SD card. He copied all the files to my flash drive, but this left me without a nice, tidy directory structure. Normally, I organize my photos like:

2008
   2008_06_27
   2008_06_28
   2008_06_29

First off, I needed to list the files by date:

ls -lt

This showed me that I had files from the 27th, 28th, and 29th in a single directory. So I created a directory for each date.

Next, I used find with the -newer and -exec switches to execute a move command on each file newer than a particular file.

By looking at the ls -lt output, I found the last photo taken on each day. IMG_6369.JPG was the last taken on the 28th, and IMG_6284.JPG was the last taken on the 27th.

To move all of the files taken on the 28th and 29th, I used this command:

find . -type f -newer IMG_6284.JPG -exec mv {} 2008_06_28 \;

Now, all files taken after the 27th are located in the 2008_06_28 directory. Now, I change into that directory, and execute:

find . -type f -newer IMG_6369.JPG -exec mv {} ../2008_06_29 \;

Now I’m left with all the photos from the 27th in the main directory. Those could easily be moved with:

find . -maxdepth 1 -type f -exec mv {} ../2008_06_27 \;

I’m sure there is a quicker, cleaner way to do this, but for the limited number of subdirectories I had to filter into, this was reasonably efficient.

About Jeff Fitzsimons

Jeff Fitzsimons is a software engineer in the California Bay Area. Technical specialties include C++, Win32, and multithreading. Personal interests include rock climbing, cycling, motorcycles, and photography.
This entry was posted in Scripting, Technology. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *