Related link: http://www.onlamp.com/pub/a/onlamp/2005/12/15/organizing_files.html

Karl Vogel, on sister site ONLamp.com, recently published an article: “Organizing Files” which you should read. It’s a very cool review of what systems didn’t work, what almost worked, and what GTD-inspired solution wound up working for him.

In the midst of the article, Karl mentions creating a “notebook” directory containing individual year folders, each of which holds a series of folders in a “mmdd” format, one per day. There would be a ~/notebook/2005/1221 for today’s date, 2005-12-21, for example

It struck me that it would be handy to automate the process of creating that hierarchy. For my purposes, however, I’d rather have a yyyy/mm/dd format to cut down on the number of folders at any given level.

So, just for the heck of it, I wrote a shell script to create a year’s worth of folders, one per day. You can download createfolders.txt (rename it to createfolders.sh and make it executable), or check it out below.

Disclaimer: I make no claims to being a shell script ninja, so I’m sure there are other, more efficient methods that will also tuck you into bed at night and read you a bedtime story. TMTOWTDI. ;) Also, I’ve tested this using bash on my OS X 10.4.3 system. That means it may travel back in time and kill some ancestor of yours, preventing your birth and creating a rift in spacetime. You have been warned.

#!/bin/sh
# createfolders.sh
# create a date-based folder hierarchy for an entire year
# by Robert Daeley
# quick and dirty -- no error checking
# -------------------------------------------------------------
# Array for number of days in each month
# change the 28 manually for leap years
numdays=( 0 31 28 31 30 31 30 31 31 30 31 30 31 )
# change targetpath to desired location
targetpath=/Users/rdaeley/notebook/2006
# -------------------------------------------------------------
# first, a function to create a number of folders
createfolders ()
{
	g=1
# this $1 refers to the value passed to this function
	while [ "$g" -le "$1"  ]
	do
# if the month or day is 1-9, we add a leading zero
		if [ "$g" -lt 10 ]
		then
			mkdir "0$g"
		else
			mkdir "$g"
		fi
		let "g += 1"
	done
}
# -------------------------------------------------------------
# now we get started
cd "$targetpath"
m=1
# We loop through the 12 months and create a folder for each
# and, along the way, create the appropriate number of days.
# First, the months...
createfolders "12"
# ...then we run through each month and create the day folders.
while [ "$m" -le 12 ]
do
	if [ "$m" -lt 10 ]
	then
		cd "$targetpath/0$m"
	else
		cd "$targetpath/$m"
	fi
	let "thismonth = ${numdays[$m]}"
	createfolders "$thismonth"
	let "m += 1"
done
exit 0