Unix How To: Planting a Tree, Unix-Style

By  

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.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question