The mkdir command in Linux
mkdir (make directory) creates new directories. Its most valuable option by far is -p, which creates every missing directory in a path at once and does not complain if part of it already exists, which is why virtually every deployment script uses mkdir -p.
How mkdir works
mkdir maps almost directly onto the mkdir() system call: create a new directory entry, allocate an inode for it, and initialize the two entries every directory is born with, "." pointing to itself and ".." pointing to its parent. Those two entries are why an "empty" directory reports a link count of 2.
The permissions of the new directory are not simply the mode you request. The kernel applies your umask: a per-process mask of bits to remove, so the effective mode is mode AND NOT umask. With the default umask of 022, a requested 777 becomes 755. This one formula explains why freshly created files and directories "mysteriously" lack group and world write permission on virtually every system.
Syntax
mkdir [OPTIONS] DIRECTORY... Common options
| Option | What it does |
|---|---|
-p | Create parent directories as needed; no error if the directory already exists. |
-v | Verbose: print a message for each directory created. |
-m | Set permissions on the new directory directly, e.g. mkdir -m 700 private. |
How to use mkdir: examples
$ mkdir projects Create a single directory in the current location.
$ mkdir src tests docs Create several directories with one command.
$ mkdir -p projects/web/assets/css Create the whole nested path in one go, even if none of the intermediate folders exist yet.
$ mkdir -p backup/{daily,weekly,monthly} Brace expansion creates backup/daily, backup/weekly and backup/monthly at once.
$ mkdir -m 700 private Create a directory only its owner can enter, read or modify.
Real-world use cases for mkdir
Scaffolding a project in one line
Starting a new web project: mkdir -p app/{src/{components,utils},tests,docs} creates the whole tree in a single command. Brace expansion multiplies out every combination: six directories, one line, no errors if you run it twice.
Idempotent deploy scripts
A deploy script must create /var/app/releases if missing but not fail when it exists. mkdir -p does both: it is idempotent, which is exactly what automation needs. Virtually every Dockerfile and CI pipeline relies on this behavior.
Try mkdir yourself
This is a live sandbox with a small filesystem (documents/, notes.txt,
backup.sh…). Nothing you do here can break anything: experiment freely.
Try: mkdir -p projects/web/assets
Pro tips and common mistakes
- mkdir -p never errors on existing directories, that makes it safe to re-run, which is the property scripts need most.
- Create and enter in one line: mkdir -p new-project && cd $_: the $_ variable holds the last argument of the previous command.
- Set permissions at creation with -m: mkdir -m 700 ~/.ssh avoids the insecure-window between creating and chmod-ing.
- Brace expansion happens in the shell, not mkdir, so it works with any command: touch file{1..10}.txt, cp config.{yml,yml.bak}.
Frequently asked questions about mkdir
What does mkdir -p do?
It creates all missing parent directories in the given path, and it succeeds silently if the directory already exists. mkdir -p a/b/c creates a, then a/b, then a/b/c as needed.
Why do I get "No such file or directory" from mkdir?
Without -p, mkdir refuses to create a directory whose parent does not exist. mkdir a/b/c fails if a/b is missing; mkdir -p a/b/c succeeds.
Can I create multiple directories at once?
Yes: pass several names (mkdir one two three) or use brace expansion (mkdir -p project/{src,tests,docs}).
Things people do with mkdir
Related commands
Move between directories: absolute and relative paths, home shortcut, and jumping back.
touchCreate empty files instantly, or update the timestamps of existing ones.
rmDelete files and directories, and understand -r, -f and why there is no trash bin.
cpCopy files and whole directory trees, preserving attributes when needed.
Concepts behind mkdir: umask