Root
Filegroup

Filegroup

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.

Example

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 vilecore1.0.zip
-rw-rw-rw-  1 mikeg  users     3990 Dec 24  2000 vilestory.txt

$ ~/bin/group.sh *
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 vilecore1.0.zip to 2004-11-26/
Creating directory 2000-12-24/
Moving vilestory.txt to 2000-12-24/

$ find .
.
./2000-12-24
./2000-12-24/vilestory.txt
./2004-11-26
./2004-11-26/vilecore1.0.zip
./2003-04-20
./2003-04-20/VILECORE.DEH
./2003-04-20/vilecore1.0.txt
./2003-04-20/vilecore1.0.wad

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

The code will omit any arguments that are directories.

Code

Here is the script so you can use it too.

#!/bin/sh

for k in "$@"
do
  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
    fi
    echo Moving "$k" to $destdir
    mv "$k" $destdir
  fi
done

Caveat

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.

Improvements

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 https://michal.guerquin.com/filegroup.html, updated 2004-12-11 02:18 EST

Contact: michalg at domain where domain is gmail.com (more)