Quick take: The mkdir command creates directories. Add -p to create parent directories as needed (and avoid errors if they exist), and -m to set permissions at creation time.

Introduction

The mkdir command (make directory) creates one or more new directories. On its own it is trivial, but two options — -p for nested paths and -m for permissions — make it genuinely powerful for scripting and server setup.

This guide covers creating single and nested directories, building several at once with brace expansion, and setting permissions as you create.

Syntax

The basic syntax of the mkdir command is:

mkdir [OPTIONS] DIRECTORY...

Common Options and Parameters

The most useful options and parameters for the mkdir command:

OptionDescription
-pCreate parent directories as needed; no error if the directory already exists.
-m MODESet the permission mode (e.g. 755) on the new directory.
-vVerbose — print a message for each directory created.
-ZSet the SELinux security context on the new directory.

Practical Examples

Real mkdir commands you can run today:

# Create a single directory
mkdir projects
# Create a nested path in one go
mkdir -p projects/app/src
# Create several directories at once
mkdir docs images styles
# Create a structured tree with brace expansion
mkdir -p site/{css,js,img}
# Create a directory with specific permissions
mkdir -m 700 ~/private
# Verbose output
mkdir -pv build/output

Using mkdir in Scripts and Automation

mkdir shines in scripts, where the -p flag makes directory creation idempotent — safe to run repeatedly without errors. This matters in deployment and setup scripts that may run more than once.

# Build an application directory layout in one line
mkdir -p /srv/myapp/{bin,etc,var/log,var/run}

# Create a dated backup directory
mkdir -p "/backup/$(date +%Y-%m-%d)"

# Create a secure directory in a single step
mkdir -m 700 -p ~/.secrets

Brace expansion combined with -p turns a complex directory tree into a single, readable command — far cleaner than a loop, and easy to keep in version control.

Tips and Best Practices

  • Always use -p in scripts — it creates the full path and will not fail if part of it already exists.
  • Brace expansion (mkdir -p app/{bin,etc,var}) builds an entire directory tree in a single command.
  • Combine with -m to create a secure directory in one step instead of running chmod afterwards.

Final Thoughts

mkdir is a small command that earns its place through -p and -m. Use -p for nested and idempotent directory creation in scripts, brace expansion to build trees instantly, and -m to set permissions on the spot. It pairs naturally with cd, ls, and rmdir.

FAQ: mkdir Command in Linux

How do I create nested directories with mkdir?+

Use the -p flag: mkdir -p a/b/c creates a, then a/b, then a/b/c in one command. Without -p, the parent directories must already exist.

How do I create multiple directories at once?+

List them separated by spaces (mkdir one two three) or use brace expansion (mkdir -p project/{src,bin,docs}).

How do I set permissions when creating a directory?+

Use -m with the mode: mkdir -m 750 secure creates the directory with rwxr-x--- permissions directly, no separate chmod needed.

Why does mkdir say the directory already exists?+

By default mkdir errors if the target exists. Add -p to make it ignore an existing directory, which is useful in scripts that may run more than once.

What is the difference between mkdir and mkdir -p?+

Plain mkdir creates a single directory and fails if a parent is missing or the target exists. mkdir -p creates any missing parents and succeeds even if the directory already exists.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support