Unix How To: Planting a Tree, Unix-Style

By Sandra Henry-Stocker  Add a new comment

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.

ITworld LIVE

Operating SystemsWhite Papers & Webcasts

White Paper

A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

White Paper

Consolidating Lotus Domino x86 Workloads on IBM Power Systems

Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

White Paper

Task, workflow & issue management for teams. Try free!

Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

Webcast On Demand

Best Practices in Monitoring VMware

The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

Sponsor: Nimsoft

White Paper

How Nimsoft Service Desk Speeds Deployment and Time to Value

For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

See more White Papers | Webcasts

Ask a question

Ask a Question