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.
Read more: Productivity, Linux
Trackbacks & Pings
Comments
-
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 -
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. ?
-
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; -
Just fyi, this is default behaviour for the Windows version of mkdir.
so:
mkdir c:\a\b\c\d
Will create the whole tree
-
Thank you! This is immediately useful to me!

CLI Fun: Make a whole directory tree in one command at SoftSaurus on 17 Apr 2007 at 9:20 pm
[…] In that example, if the a directory already exists, mkdir won’t complain, it’ll just go on and create b and c under it. Handy. —Gina Trapani Quick Tip: Create a Whole Dir Path with One Stroke [CodeJacked] […]
links for 2007-04-20 | Musings of a Chicagoan on 19 Apr 2007 at 10:24 pm
[…] Quick Tip: Create a Whole Dir Path with One Stroke “Just do this: mkdir -p a/b/c.” (tags: reference useful geekery cool) […]
error is the mother of all inventions on 10 May 2007 at 12:55 am
[…] this way >> No Comments so far Leave a comment RSS feed for comments on this post. TrackBack URI Leave a comment Line and paragraph breaks automatic, e-mail address never displayed, HTMLallowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> […]
Top 10 Command Line Tools [Lifehacker Top 10] · TechBlogger on 30 Jul 2008 at 9:43 am
[…] When it comes to organizing music, pictures, documents, or other media, nested folders become a necessary annoyance—as in right-clicking, choosing “New Folder” and then naming and clicking through each of “The Beatles->White Album->Disc 1.” It’s far easier from the terminal, as the Codejacked blog points out: mkdir The BeatlesWhite AlbumDisc 1 […]