How to Create a Hierarchy of Directories

It is a simple matter to create a hierarchy of directories, also called a directory tree, with a single command in a Unix-like operating system. This can be more convenient and provide greater consistency of the directory structure than issuing a series of separate commands or creating the structure on an ad hoc basis.

Creation of an entire directory tree can be accomplished with the mkdir command, which (as its name suggests) is used to make directories. The -p option tells mkdir to create not only a subdirectory but also any of its parent directories that do not already exist.

For example, to create the hierarchy of directories 2006/seattle/plans/marketing beginning in the current directory (i.e., the directory in which the user is currently working), all that is necessary is to issue the following command:

mkdir -p 2006/seattle/plans/marketing

This single command takes the place of a minimum of four and as many as seven separate commands, i.e., creating four separate directories with mkdir and changing into the first three of them with the cd (i.e., change directory) command.

Branches can easily be added without having to change the current directory. For example, a branch portland/plans/marketing that begins in the directory 2006 could be added with the following:

mkdir -p 2006/portland/plans/marketing

A multi-branched directory tree can be created with a single command rather than with a separate command for each branch. This is accomplished by following mkdir -p with the path (i.e., sequence of directories) of each branch beginning with the current directory. Thus, for example, the tree beginning with 2006 and having the two branches seattle and portland can be created as follows:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing

Any number of subdirectories or branches can be created simultaneously by including them in the same command. For example, the following would additionally create a branch called sales/results that begins in the seattle subdirectory:

mkdir -p 2006/seattle/plans/marketing 2006/portland/plans/marketing 2006/seattle/sales/results

Each branch is separated by a single space, and the entire command is placed on a single line, although on narrow screens it might appear to be on multiple lines.

If using a GUI (graphical user interface), it can be convenient, particularly in the case of larger or more complex trees, to first write (and edit as needed) the command with a text editor, such as gedit or kedit, and then use the copy and past functions to transfer it to the terminal window (i.e., an all-text mode window in a GUI).

The contents of the new hierarchy of directories can be viewed with a single command by using the ls command with its -R (i.e., recursive) option. For the above example this can be accomplished with the following:

ls -R 2006

A disadvantage of this command is that the results can be spread out over numerous lines and not easy to read. A more concise view can be obtained with the du (i.e., disk usage) command, which is commonly used to show the sizes of directories. For example, du could be used with the directory tree created in the above example as follows:

du 2006

The du command has the additional advantage of showing the size of each directory, which can be useful if files are added to the directories. The default unit is kilobytes.

If an error has been made in creating the hierarchy of directories, it is easy to remove the entire hierarchy by using the rm command with its -r option. For example, the following would delete the entire 2006 directory tree, including any files in it:

rm -r 2006

rm -r is a very powerful -- and dangerous -- command, and thus it should be used with caution.

Created July 1, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.