Linux Magic

Let’s say you want to make a directory tree in Linux a few directories deep. You could do this:

greg@greg-laptop:~$ mkdir tst
greg@greg-laptop:~$ cd tst
greg@greg-laptop:~/tst$ mkdir tstdeeper
greg@greg-laptop:~/tst$ cd tstdeeper
greg@greg-laptop:~/tst/tstdeeper$ mkdir evendeeper
greg@greg-laptop:~/tst/tstdeeper$ cd evendeeper

Or, you could be smart and do this:

greg@greg-laptop:~$ mkdir -p tst/tstdeeper/evendeeper

Notice the -p option.


  1. #1 StuV
    March 3, 2008

    For the record, Windows does this magically — without the need for -p.

    Just saying.

  2. #2 kezdro
    March 3, 2008

    On the other hand, windows doesn’t let you use ‘?’ or several other characters.

  3. #3 Greg Laden
    March 3, 2008

    Yet another major security hole in windows exposed!

  4. #4 Jeff Darcy
    March 3, 2008

    The other nice thing about -p is that it doesn’t complain if the target exists. This makes it very useful in situations where you want to ensure a directory is there and you don’t want to deal separately with the case where it is vs. the one where it isn’t.

  5. #5 rpenner
    March 3, 2008

    Beware the power of xargs.

    sed -e ‘s/\(.\)/\/\1/g’ -e ‘s/^/words/’ /usr/dict/words | xargs mkdir -p