mkdir Command Explained
mkdir creates directories. It is a small command with a small number of flags, but a couple of those flags come up constantly enough that they are worth knowing cold.
Basic usage
mkdir projects
mkdir "My Documents" # quote names containing spaces
mkdir docs images scripts
# creates all three directories in one command
Running mkdir with a name that does not yet exist in the current directory creates it as an empty folder. Multiple names given as separate arguments all get created in a single invocation.
The -p flag: creating nested paths
mkdir projects/2026/reports
# mkdir: cannot create directory 'projects/2026/reports': No such file or directory
mkdir -p projects/2026/reports
# succeeds, creating projects/, projects/2026/, and projects/2026/reports/ all at once
Without -p, mkdir expects every parent directory in the path to already exist; it only creates the final component. This is the single most common mkdir error beginners run into. Adding -p (which stands for “parents”) tells mkdir to create every missing directory along the way, and critically, it also stops treating “the directory already exists” as an error, which makes it the safer choice for scripts:
# Safe to run repeatedly, never errors just because the folder exists
mkdir -p ~/backups/daily
Setting permissions at creation time: -m
mkdir -m 700 private_notes
ls -ld private_notes
# drwx------ 2 colton colton 4096 Jul 9 10:00 private_notes
Without -m, a new directory is created with permissions determined by your current umask setting, typically resulting in 755 or 775. If a directory needs restricted permissions from the very first instant it exists, for example one meant to hold sensitive data, using -m avoids the brief window where the directory would otherwise sit at the broader default permission before a follow-up chmod locks it down.
Creating multiple nested directories with brace expansion
mkdir -p project/{src,docs,tests}
ls project
# docs src tests
mkdir -p project/src/{main,test}/{java,resources}
ls -R project
Brace expansion is a shell feature, not specifically a mkdir feature, but the two combine constantly in practice. It lets you scaffold an entire directory tree for a new project in a single command instead of running mkdir repeatedly for each subfolder.
Common errors and what they mean
mkdir existing_folder
# mkdir: cannot create directory 'existing_folder': File exists
This happens when the target already exists as either a file or a directory. If you just want to guarantee the directory exists without caring whether it was already there, use mkdir -p, which treats an already-existing directory (not a file with the same name) as success rather than an error.
mkdir /etc/newconfig
# mkdir: cannot create directory '/etc/newconfig': Permission denied
This means you lack write permission on the parent directory, /etc in this case. Most locations outside your own home directory require sudo to create new entries in:
sudo mkdir /etc/newconfig
mkdir a/b/c
# mkdir: cannot create directory 'a/b/c': No such file or directory
As covered above, this means one or more parent directories in the path do not exist yet. Add -p to create the full chain in one step.
Verbose output
mkdir -pv project/{src,docs,tests}
# mkdir: created directory 'project'
# mkdir: created directory 'project/src'
# mkdir: created directory 'project/docs'
# mkdir: created directory 'project/tests'
The -v flag prints a line for every directory actually created, which is useful for confirming exactly what a -p invocation did, especially when some of the requested directories already existed and were silently skipped.
Frequently Asked Questions
What does the mkdir command do?
mkdir creates one or more new, empty directories at the path or paths you specify. It is the standard way to create folders from the command line, equivalent to right-clicking and choosing New Folder in a graphical file manager.
What does mkdir -p do and when should I use it?
mkdir -p creates a full path of nested directories in a single command, automatically creating any parent directories that do not yet exist, and it does not error if the target directory already exists. Without -p, mkdir a/b/c fails immediately if a and a/b do not already exist, because mkdir by default expects the parent path to already be present.
Why does mkdir fail with “No such file or directory”?
This error means you tried to create a directory whose parent path does not exist yet. Running mkdir projects/2026/reports fails with this error if the projects/2026 directory has not been created first, because plain mkdir only creates the final directory in the path, not any missing parents. Adding -p resolves this by creating every missing parent directory along the way.
How do I set permissions on a directory at the moment I create it?
Use the -m flag followed by the desired permission mode, for example mkdir -m 700 private_folder, which creates the directory with those exact permissions immediately, rather than creating it with default permissions and then running a separate chmod command afterward. This closes a brief window where the directory might otherwise exist with broader default permissions before you lock it down.
Can mkdir create multiple directories at once?
Yes, list multiple directory names or paths as separate arguments, such as mkdir docs images scripts, which creates all three in one command. Combined with brace expansion, mkdir -p project/{src,docs,tests} creates a project directory containing three subdirectories, src, docs, and tests, in a single line.
What is the difference between mkdir and mkdir -p in terms of error handling?
Plain mkdir exits with an error if the target directory already exists or if a required parent directory is missing. mkdir -p silently succeeds if the target directory already exists (treating it as success rather than an error) and automatically creates any missing parent directories rather than failing. This makes mkdir -p the safer default for scripts, where you generally want “ensure this directory exists” behavior rather than “fail if it is already there.”