Mac OS X,  Mac OS X Server,  Unix

Creating Directories Using -p with mkdir

I was helping someone out the other day and was VNC’d into their server watching them work.  I asked them to make a directory about 5 levels deep into the file system.  They proceeded to make each directory one by one using mkdir.  When they were done, I actually removed them all so I could spend a second to show them how to do so much quicker…  Let’s say the directory you need created is /tmp/usr/tmp.  Rather than using 3 mkdir commands and cd’ing in between it would actually behoove one to go about using the -p option with the mkdir command.  To make this directory structure you could actually just use:

mkdir -p /tmp/usr/tmp

Now let’s say you wanted to create a file in this directory called test.txt.  Well, you could then just run the following command to make the directory:

touch !$/test.txt

Now you have done what could otherwise take 10 commands in 2.  Work smart, not hard.  😉