Move files to directories based on modification time

Here’s a really simple shell script to move files to directories named based on the modification time of each file. It uses only the date and the script also accepts an optional first argument to set a suffix for each directory. I sometimes use this as a first step when I organize photos and videos, the suffix is set to the source of the files (user-device in my case, eg ckk-40d if it’s from my Canon 40D), I then manually add a short description to the end based on the directory contents. Most commonly applied when I empty the card from my point-and-shoot.

#!/bin/sh

# Put files on command line in one directory per date (based on mtime) with the
# suffix given by the first optional argument on the command line

if [ ! -n "$1" ]; then
   echo "Usage:"
   echo "$0 [suffix]  ... "
   exit
fi

if [ ! -f "$1" ]; then
  suffix=$1
  shift
fi

while [ -n "$1" -a -f "$1" ]; do
  mdate=`stat --printf "%y" $1 | cut -d\  -f1`
  mkdir -p $mdate$suffix
  mv -v $1 $mdate$suffix
  shift
done