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!

  6. anyone knows how to create a script that will produce a file of the pwd to text? I am at my whits end on figuring this out and i was told that did my script wrong. this is what i have:

    #!/bin/bash
    cd / pwd >>
    ls -lt >>

    really don’t know if I am doing this right or what, but I could use all the help I can get.

Post a Comment


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



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