How to Create a Directory and Its Parent Directories in Linux

Use mkdir -p to create a directory tree in one command without errors for existing parents.

Plain `mkdir` expects parent directories to exist. For nested paths, `mkdir -p` is the standard solution because it creates every missing component.

Step by step

1 Create one directory

$ mkdir project

Creates a single directory when its parent already exists.

2 Create a complete path

$ mkdir -p project/src/components

Creates project, src and components as needed.

3 Create several directories

$ mkdir -p logs/{app,nginx,archive}

Brace expansion can create several sibling directories in Bash.

4 Verify the result

$ find project -maxdepth 3 -type d

Lists the directory tree so you can confirm the path was created where expected.

Tips worth knowing

Frequently asked questions

What does mkdir -p do?

It creates missing parent directories and does not error if the target directory already exists.

Why does mkdir say No such file or directory?

A parent directory in the requested path does not exist; use `mkdir -p`.

The commands behind it

mkdir

Create one or many directories, including whole nested paths with -p.

pwd

Show the absolute path of the directory you are currently in.

ls

List the contents of a directory, with options for hidden files, long format, sorting and more.

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.