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
- Use absolute paths carefully; a typo can create directories in an unexpected location.
- Quote paths with spaces.
- mkdir -p is safe to rerun when the directory already exists.
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
Create one or many directories, including whole nested paths with -p.
pwdShow the absolute path of the directory you are currently in.
lsList the contents of a directory, with options for hidden files, long format, sorting and more.
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.