How to Use the Recursive Linux Make Directory Command

Generally, when you use the mkdir Linux make directory command you create a single subdirectory that lives in whatever directory your prompt is currently sitting in. If you were in ~/Documents and you typed mkdir Memoranda, then you’d create a single directory called Memoranda that lived in ~/Documents. You don’t usually create more directories inside of it.

However, you can use the recursive form of the Linux make directory command to create entire directory trees. You can create a directory inside the directory that you’re sitting in and then make many other directories inside of that. Naturally, you’ll need to be working from a CLI prompt to continue. Hold down Ctrl, Alt and T to open a graphical terminal. You could also search for Terminal on the Ubuntu Unity Dash or select the Applications menu, click on System Tools and select Terminal. You won’t have to be working as a root user if you’re not making directories outside of your own home directory.

Method 1: Using the Parent mkdir Option

If you wanted to make a number of directories all at once, then you could type mkdir -p hey/this/is/a/whole/tree and then push enter. You’d get an entire set of directories with each of those names, all nested inside of each other. Obviously, you could use whichever name you’d like at any point in the tree. If some of those directories exist, say there already is hey and this but not the others, then mkdir will simply pass these over without error and make directories underneath them.

The -p option is called parents, and could theoretically be invoked in many distributions by typing –parents instead of -p in the previous command. You can create a practically unlimited number of directories in this fashion all at once. As soon as they’re created, they function completely like any other directories. This means that if you try to remove the top one, it will complain about not being empty too!

Method 2: Using the Parent mkdir Option Plus Brace Expansion

Brace expansion allows you to create a bunch of directories that follow a single pattern when using the bash command interpreter. For instance, if you typed mkdir {1..4}, then you will have created four directories numbered as such in the current directory. If you wanted to, then you could combine this concept with the parent option. You could, for instance, type mkdir -p 1/{1..4} and push enter to create a directory called 1 with directories called 1, 2, 3 and 4 inside of it. It’s a very powerful command, and you can use it to create tons of directories all at once. This makes it perfect for sorting collections of photos, videos and music in Linux. Some people also use this technology when creating install scripts for software or packages they plan to distribute.

You can of course mix this option in and add brace expansion to any part of the command. If you wanted to create some directories via brace expansion, and then others via only parents recursion, then you might want to try a command like mkdir -p a/directory/inside{1..4}, which will create a and directory inside of a as well as inside1, inside2, inside3 and inside4 underneath it. Feel free to experiment a bit and create extra directories inside of each other once you’ve already learned how to use the mkdir command, but keep in mind that you won’t be able to remove directories that have other directories inside them without a little recursion or the use of a file manager.

Kevin Arrows
Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.

Expert Tip

How to Use the Recursive Linux Make Directory Command

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested