Making Nested Directories in Linux

Creating nested directories when the parent directory does not exist is... impossible. But that also does not mean that you need to create the parent directory first and invoke mkdir again.

There is a faster way to achieve this; you can use the -p flag with mkdir command.

mkdir -p parent_dir/child_dir/nested_child_dir

Using the -p flag will let mkdir know that it is okay to make a parent directory if it does not exist yet, and then create a nested child directory.

mkdir syntax

The method for creating directories in UNIX-like systems is to use the mkdir command.

Below is the syntax of mkdir:

mkdir [OPTION] DIRECTORY

Create nested directories with the '-p' flag

Just as an example, I want to create a directory structure where the parent directory's name is 'songs' and the child directory's name is 'artists.'

The typical way to achieve this is by the following set of commands:

$ mkdir -v songs
mkdir: created directory 'songs'

$ mkdir -v songs/artists
mkdir: created directory 'songs/artists'

# I only included the '-v' flag for verbosity, you need not use it

This can be achieved in a single line like so:

$ mkdir -p -v songs/artists
mkdir: created directory 'songs'
mkdir: created directory 'songs/artists'

But... you said, "it is impossible." Technically, you can not create a child directory if the parent does not exist. Yes, that still holds true.

So, let us understand what happened here.

The mkdir command has a '-p' flag, and its descriptor is "no error if existing, make parent directories as needed". The second part of that statement says that mkdir will create a parent directory if the need be.

If you look at the output of mkdir in the second example, you will notice that mkdir created the 'songs' directory first and then created 'artists' under the 'songs' directory.

This indicates that mkdir will first make the necessary parent directories and then the child directory if you use the '-p' flag.

Create multiple child directories under a not-yet-existing parent directory

Let's try to create multiple child directories when the parent directory does not exist.

There are two ways to achieve this. Have a look:

$ mkdir -vp songs/artists songs/albums songs/genres songs/genres/classical songs/genres/pop
mkdir: created directory 'songs'
mkdir: created directory 'songs/artists'
mkdir: created directory 'songs/albums'
mkdir: created directory 'songs/genres'
mkdir: created directory 'songs/genres/classical'
mkdir: created directory 'songs/genres/pop'

Or, you can use the following command:

$ mkdir -vp songs/{artists,albums,genres/{classical,pop}}
mkdir: created directory 'songs'
mkdir: created directory 'songs/artists'
mkdir: created directory 'songs/albums'
mkdir: created directory 'songs/genres'
mkdir: created directory 'songs/genres/classical'
mkdir: created directory 'songs/genres/pop'

As you can see, both commands have the same output, but the latter one is faster to type.

The '{}' curly brackets are used to specify a list, in this case, a list of directory names.

In this example, the parent directory 'songs' does not exist, so it was created. Then, the child directories 'artists', 'albums', and 'genres' are created. The directories 'classical' and 'pop' are listed under genres, so these two are the child directory of 'genres,' and they are created as I expected.

Conclusion

You can learn more about the mkdir command in the article below.

mkdir command: Create New Directories in Linux
mkdir is one of the essential Linux commands that every Linux user should know. You can create new directories using mkdir.

So, now you know how you can create a child directory when a parent directory does not exist - without creating each directory manually.