Thursday, May 28, 2009

How to make many levels of directories easily?

This does happen from time to time. You need to create a many levels of directories on your system like this:

/someroot/dir1/dir2/dir3/dir4

You could use mkdir (make directory, see man mkdir) to create them one at a time, then cd into the one you created and create the next one. That would look like this:

> mkdir someroot
> cd somroot
> mkdir dir1
> cd dir1

You get the picture. That's a lot of typing and a waste of time.

Fortunately there is an easier way to do it. The mkdir command has a switch '-p' that will create intermediate directories if they don't already exist. Using the '-p' switch our command would look like this:

> mkdir -p /someroot/dir1/dir2/dir3/dir4

That's much better than creating them one at a time.

By the way, I should mention that cd is the command used to change directories.
See the man page for cd (man cd).


No comments: