I have a ton of files in my tmp directory. It's hard to make sense of them. ls isn't good enough when the entire list fills multiple screenfulls.

So I wrote a little sh script to categorize files into directories according to the time of their creation.


Sample output looks like so:

$ ls -l
total 7310
-rw-rw-rw-  1 mikeg  users     2239 Apr 20  2003 VILECORE.DEH
-rw-rw-rw-  1 mikeg  users     4946 Apr 20  2003 vilecore1.0.txt
-rw-rw-rw-  1 mikeg  users  5568032 Apr 20  2003 vilecore1.0.wad
-rw-r--r--  1 mikeg  users  1857351 Nov 26 13:30
-rw-rw-rw-  1 mikeg  users     3990 Dec 24  2000 vilestory.txt

$ ~/bin/ *
Creating directory 2003-04-20/
Moving VILECORE.DEH to 2003-04-20/
Moving vilecore1.0.txt to 2003-04-20/
Moving vilecore1.0.wad to 2003-04-20/
Creating directory 2004-11-26/
Moving to 2004-11-26/
Creating directory 2000-12-24/
Moving vilestory.txt to 2000-12-24/

$ find .

The above sample was very contrived, but it illustrates the principle.

The code will omit any arguments that are directories.


Here is the script so you can use it too.


for k in "$@"
  if [ -f "$k" ]; then
    export `stat -s -t "%Y-%m-%d" "$k"`
    destdir=`date -r $st_birthtime +"%Y-%m-%d"`/
    if [ ! -d $destdir ]; then
      echo Creating directory $destdir
      mkdir $destdir
    echo Moving "$k" to $destdir
    mv "$k" $destdir


The code was made to run on FreeBSD. That is, the options passed to stat and date may not be compatible with your version of Linux or UNIX.


Links: Rather than moving files to new directories, create symbolic (or hard) links in date directories.

Directories: Move (or link, as per above) entire directories just like files.

This is, updated 2004-12-11 02:18 EST

Contact: michalg at domain where domain is (more)