Quick Tip: Create a Whole Dir Path with One Stroke

The UNIX mkdir has a -p option that will automatically make any parent folders that do not already exist. So, instead of doing this:

mkdir a
mkdir a/b
mkdir a/b/c

just do this:

mkdir -p a/b/c

Not only is this a timesaver, it’s also considered good form. It’s more succinct, and it better expresses the idea that the a and b folders might already exist.

Trackbacks & Pings

Comments

  1. Does any one know how to make it step by 1
    example :
    mkdir -p 1/2/3/4/….. etc I need it to go to 1000 and ctrl c I could stop it

  2. Ace, you could write a bash script to do this incrementing for you. I’m not very eloquent in bash, but it shouldn’t be too hard to figure out.

    Just not sure why you’d want to have nested folders hundreds of levels deep. ?

  3. Its quite simple. You can do the following.

    #!/bin/bash

    i=1

    while [ $i -le 100 ]; do
    mkdir $i;
    cd $i;
    i=$((i+1))
    done;

  4. Just fyi, this is default behaviour for the Windows version of mkdir.

    so:

    mkdir c:\a\b\c\d

    Will create the whole tree

  5. Thank you! This is immediately useful to me!

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.