Unix How To: Planting a Tree, Unix-Style

Most people learn the mkdir command on the first day they start using Unix. Along with ls, cd and pwd, it's in the set of the most basic commands that everyone who ventures onto the command line on a Unix system ought to know. It generally takes a while before they learn the more exotic uses of this simple command -- such a creating a branching tree structure with just one mkdir.

The simplest form of the mkdir command is, of course, creating a single directory.

mkdir mydir

As novice Unix users begin to pick up relative addressing and environment variables, they will probably figure out that they can take their directory creating skills a little further and may try commands such as these -- creating a directory inside an existing directory (without moving into the directory first), creating a directory in their home no matter where they're starting from, creating a directory in the parent directory, creating temporary directories (with or without randomized names) and creating a directory using the output of Unix commands to name them.

mkdir mydir/newdir
mkdir ~/mydir
mkdir ../mydir
mkdir /tmp/$USER
mkdir /tmp/$USER$$
mkdir `date +%m%d%y`

Sometime later, new Unix users will likely run into the -p option that allows them to create deeply embedded directories without first having to create all the intermediate folders. Ah, the joy of Unix!

mkdir -p mydir/a/b/c/d/e

It isn't until the mkdir command is combined with brace expansion that things get really exciting (and a little hard to parse!). Once you add {a,b,c} type syntax to a mkdir -p command, you can create a branching tree rather than just a straight sequence of directories inside directories.

Create multiple directories with one command:

mkdir -p {onedir,twodir,threedir}

Create multiple directories inside a new directory with one command:

mkdir -p mydir/{onedir,twodir,threedir}

Create multiple directories inside a new (today's date) directory with one command:

mkdir -p `date +%m%d%y`/{bin,data,doc,refs}

Create a fairly complex directory structure with one command:

mkdir -p `date +%m%d%y`/{bin,data/{raw,proc},doc/{config,man},refs}

To grasp what this command is doing, it helps to start by breaking out the items in the list inside the outer brackets, ignoring the contents of the inner brackets for now.

mkdir -p `date +%m%d%y`/{bin,data/{raw,proc},doc/{config,man},refs}
                         --- =============== ---------------- ====

So, this command will create directories named bin, data, doc and refs within the dated directory.

Then, you can break out the contents of the inner braces.

mkdir -p `date +%m%d%y`/{bin,data/{raw,proc},doc/{config,man},refs}
                         --- =============== ---------------- ====
                                   --- ====       ------ ===

Now we can start to see that we're creating a directory structure that looks like this:

         +- bin
         |        +- proc
051210 --+- data -+
         |        +- raw
         |        +- config
         +- doc --+
         |        +- man
         +- refs

Since the structure is created in a single command, you could set it up as an alias if you care to. In any case, it makes fairly simple work out of setting up a set of standard folders for users or projects.

This article is published as part of the IDG Contributor Network. Want to Join?

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon