Create Directories Recursively With mkdir

Create Directories Recursively With mkdir

The short answer

On Unix-like operating systems such as Linux and macOS, you can recursively create multiple nested directories at once using the [.inline-code] mkdir[.inline-code]  command with the [.inline-code] -p[.inline-code]  flag as follows:

 $ mkdir -p <path>


  • [.inline-code] path[.inline-code]  is a path containing a list of nested directories separated by a slash character.

For example:

 $ mkdir -p app/tmp/logs

This command will first create the [.inline-code] app[.inline-code]  directory within the local directory, then create the [.inline-code] tmp[.inline-code]  directory within the [.inline-code] app[.inline-code]  directory, and finally create the [.inline-code] logs[.inline-code]  directory within the [.inline-code] tmp[.inline-code]  directory.

Note that if an intermediate directory already exists, no error will be reported, and the [.inline-code] mkdir[.inline-code]  command will simply continue its operation.

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code] mkdir recursive[.inline-code]  in the AI Command Suggestions will prompt a [.inline-code] mkdir[.inline-code]  command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#enable-verbose-output] Enabling verbose output [#enable-verbose-output]

Since, by default, the [.inline-code] mkdir[.inline-code]  command silently skips existing directories, you can enable the verbose mode using the [.inline-code] -v[.inline-code]  flag, which will output the list of directories as they are created:

 $ mkdir -v -p <path>

For example:

 $ mkdir -p app/tmp/logs

The output of this command indicates that only the [.inline-code] logs[.inline-code]  directory was created as the [.inline-code] app[.inline-code]  and [.inline-code] tmp[.inline-code]  directories already exist.

[#create-multiple-directories] Creating multiple directories in a parent directory [#create-multiple-directories]

To recursively create multiple directories at once within the same parent directory, you can use the following syntax:

$ mkdir -p <path> …

For example:

$ mkdir -p src/public src/utils tmp/logs

This command will create the [.inline-code] src[.inline-code]  and [.inline-code] tmp[.inline-code]  directories within the same parent directory, including their intermediate directories [.inline-code] public[.inline-code] , [.inline-code] utils[.inline-code] , and [.inline-code] logs[.inline-code] .

[#common-errors-and-pitfalls] Common errors and pitfalls [#common-errors-and-pitfalls]

When creating directories using the [.inline-code] mkdir[.inline-code]  command, there are several common errors and pitfalls that you should be aware of in order to avoid running into issues.

[#insufficient-file-permissions] Insufficient file permissions [#insufficient-file-permissions]

One of the most common errors is when attempting to create a directory within a directory you don't have write and execute permissions on.

$ mkdir -p app/tmp
mkdir: app/tmp: Permission denied

To check the permissions of a directory, you can use the [.inline-code] ls -l[.inline-code]  command within its parent directory as follows:

 $ ls -l
drwxr-xr-x  3 john  staff     96 Oct 24 17:07 app

Which will output the permissions for the owner, the group, and the other users.

You can learn more about permissions with our articles on Unix file permissions and how to use the chmod command.

[#filename-conflicts] Conflict with existing files [#filename-conflicts]

Another common error is when attempting to create a directory within a directory that already contains an entry with the same name.

For example, if the target directory contains a directory with the same, the [.inline-code] mkdir[.inline-code] command will throw a "File exists" error:

$ mkdir -p app
mkdir: app: File exists

On the other hand, if the target directory contains a regular file with the same name, the [.inline-code] mkdir[.inline-code]  command will throw a "Not a directory" error:

$ mkdir -p app/tmp
mkdir: app: Not a directory

[#syntax-errors] Syntax errors [#syntax-errors]

Finally, you should avoid using special characters, spaces, or non-standard characters in the directory path as it could lead to unexpected results. For example, the following characters have a special meaning for the shell and should be avoided: [.inline-code] <[.inline-code] , [.inline-code] >[.inline-code] , [.inline-code] |[.inline-code] , [.inline-code] \[.inline-code] , [.inline-code] :[.inline-code] , [.inline-code] ([.inline-code] , [.inline-code] )[.inline-code] , [.inline-code] &[.inline-code] . For that it is recommended to only use lowercase and uppercase characters, numbers, dots, and replace spaces with underscores ([.inline-code] _[.inline-code] ) and hyphens ([.inline-code] -[.inline-code] ).

[#create-directories-with-scripts] Recursively creating directories using scripts [#create-directories-with-scripts]

For large scale or collaborative projects, scripts are often used to create complex and nested directory structures in order to save time and ensure consistency and reproducibility on different environments, thus mitigating the risk of human error.

In short, scripts allow developers to easily set up projects by automating the creation of their initial directory structure, dynamically creating directories depending on conditions or variables, or creating directories in bulk using loops and patterns.

[#create-directories-with-bash] Using a Bash script [#create-directories-with-bash]

To create nested directories in Bash, you can use a [.inline-code] for[.inline-code]  loop that iterates on the elements of an array of paths and executes the [.inline-code] mkdir[.inline-code]  command for each of these paths as follows:


directories=("app/src" "app/tmp/logs")

for directory in "${directories[@]}";
  mkdir -v -p $directory

When executed, this script will generate the following output:

 $ ./script.sh

[#create-directories-with-python] Using a Python script [#create-directories-with-python]

To create nested directories in Python, you can use the [.inline-code] makedirs[.inline-code]  method of the [.inline-code] os[.inline-code]  package as follows:

import os


Note that if any of these directories already exists, the [.inline-code] makedirs[.inline-code]  method will throw an error.

This can be avoided by using the "exist_ok" parameter as follows:

import os

os.makedirs("app/tmp/logs", exist_ok = True)