create nested directories in linux

How to Create Nested Directory in Linux with One Command

Sometimes you may need to create nested directories in Linux with a single command. Typically if you try to create a subdirectory where a parent directory does not exist you will get an error. So developers and system administrators tend to create parent directory first and then the subdirectory. But if you need to create a deeply nested directory that has many ancestors, it will be tedious. In this article, we will learn how to create nested directory in Linux using single mkdir command.


How to Create Nested Directory in Linux with One Command

Let us say you want to create a nested directory /foo/bar/baz where /foo, and /foo/bar don’t exist, then here is a simple mkdir command to create /foo, /foo/bar and /foo/bar/baz using single command.

$ mkdir -p /foo/bar/baz

In the above command, you need to mention -p option followed by the innermost nested directory to be created. In this mkdir will automatically check and create its parent directories if they don’t exist.

Typically, -p option is used with mkdir in case you want to it to create directory only if it doesn’t exist. But you can also use it for creating nested directories.

In some implementations such as GNU, mkdir command supports –parents option which is more readable than -p but it may not be available on MacOS, various BSDs and other commercial distributions.

In fact, you can even create multiple nested directories with a single mkdir command, using regular expressions in your folder name/path.

Let us say you want to create the following nested folder structure.

work
     -F1
       -temp1
       -temp2
     -F2
       -temp1
       -temp2
     -F3
       -temp1
       -temp2

You can build the above directory structure using the single command below.

$ mkdir -p work/{F1/{temp1,temp2},F2,F3}

When we specify multiple folder/subfolder names in curly braces, mkdir will create separate directory for each of them. For example, {F1/{temp1,temp2},F2,F3} would create directories F1, F2, and F3. {temp1, temp2} would result in temp1 and temp2 subfolders created for F1, F2, F3. Please note, although we have not specified temp1 and temp2 for F2 and F3, they will be created since they are part of the same group as F1, and we have specified temp1 and temp2 subfolders to be created for F1.

It is important to note that the expansion of curly braces is done by the shell, due to brace expansion feature, and not the mkdir command. It is just that when you combine -p option with brace expansion it allows you to create huge and complex directory structure in just a single command.

In this article, we will learn how to create nested directories using single command in Linux.

Also read:

How to Create CA Bundle from CRT Files for SSL Certificate
How to Copy Files from Linux to Windows
How to Copy from Linux to Amazon S3
How to Fix Permission Denied Error While Using Cat Command
How to Limit User Commands in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *