How to Create an Entire Directory Path with One Command

A Linux dedicated server has a hierarchical directory structure.  Files are stored in directories, and those directories are also stored in directories, forming an entire directory trees.  For example, documents that appear on a website may be housed in the /home/lightning/www/html directory path (“lightning” being the username of the website owner).

In order to keep information organized, it is best to maintain some type of system for arranging files within the directory tree.  For example, you may wish to have photos stored according to Date, Location, and Category.  The directory structure may look like /Photos/201104/Park/Soccer.  Normally, to create each directory in a new tree, you would need to make each one individually with the mkdir command (i.e. mkdir Photos, mkdir 201104, and so on).

By adding the “-p” option to the command (p standing for “parents”), you can create entire directory paths on the fly.  Where mkdir would normally give you an error about the parent directories not existing, the “-p” option will create them as needed.  For example, to make the aforementioned path, you would type:

mkdir -p Photos/201104/Park/Soccer

This is very useful when you need to drill down through several layers of your hierarchy but do not want to have to stop and create parent directories along the way.  It is particularly useful for extremely long paths and the creation of complex directory trees.  For more information about mkdir, type “man mkdir” from the shell prompt.











Comments: