mkdir
Create new directories
By CMD Script Team · 4 min read · Last updated
mkdir [OPTIONS] DIRECTORY...Options
| Flag | Description |
|---|---|
-p | Create parent directories as needed; don't error if the directory already exists |
-m MODE | Set the permission mode of the new directory at creation time |
-v | Print a message for each directory created |
--parents | Long form of -p |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
mkdir ("make directory") creates one or more new directories at the given paths. By
default it requires that all parent directories already exist and fails if the target
directory is already present; the -p flag relaxes both restrictions.
Beginner examples
mkdir project— create a single new directory in the current locationmkdir docs tests build— create several sibling directories in one commandmkdir -p project/src/components— create a nested path, creating any missing intermediate directoriesmkdir -v newdir— print confirmation as the directory is created
mkdir -p project/src/components
Advanced examples
- Set permissions at creation time instead of a follow-up
chmod:mkdir -m 700 ~/.ssh-backup. - Create a whole nested project skeleton in one call:
mkdir -p app/{src,tests,docs}(brace expansion combined with-p). - Safely re-run directory-creation steps in idempotent scripts:
mkdir -p buildwon't error even ifbuildalready exists, unlike plainmkdir build. - Combine with
findto bulk-create a mirrored directory structure:find source -type d -exec mkdir -p dest/{} \;(conceptually; typically done withrsync --dirs-only/cp -r --parentsin practice).
mkdir -p app/{src,tests,docs} && mkdir -m 750 app/config
Common mistakes
- Forgetting
-pwhen creating a nested path and getting "No such file or directory" because an intermediate parent directory doesn't exist yet. - Assuming
-m MODEis the final permission — the process'sumaskcan still mask out bits from the requested mode, so the resulting permissions may be more restrictive than expected. - Running plain
mkdir dirnamein a script expecting it to be a no-op if the directory already exists — it errors out instead; use-pfor idempotent scripts. - Confusing
mkdir's inability to create files with the need for a separatetouchor redirect —mkdironly ever creates directories, never regular files.
Tips
- Use
mkdir -pby default in scripts and automation for idempotency — it's safe to re-run without erroring if the directory already exists. - Brace expansion (
mkdir -p project/{src,tests,docs}) is a fast way to scaffold several directories in one command. - Check the resulting permissions with
ls -ld dirnameafter using-m, sinceumaskcan still restrict the actual bits applied.
Best practices
- Prefer
mkdir -pover plainmkdirin deployment and provisioning scripts so reruns don't fail on a directory that already exists from a previous run. - Set restrictive permissions at creation time with
-mfor sensitive directories (e.g.mkdir -m 700 ~/.secrets) rather than creating the directory openly and locking it down afterward, which leaves a brief window with looser permissions. - Keep directory-creation logic and permission-setting logic together in scripts so it's clear at a glance what access a new directory is meant to have.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Scaffolding a new project's directory structure (
src,tests,docs,build) in one command at the start of a project. - Ensuring a deployment script's target directory tree exists before copying files into
it, using
mkdir -pso re-deploys don't fail. - Creating a private directory for sensitive data with locked-down permissions from the
moment it's created, using
mkdir -m 700.
Common interview questions
- What does
mkdir -pdo differently from plainmkdir?-pcreates any missing parent directories along the path and doesn't error if the target directory already exists; plainmkdirrequires all parents to exist and fails on an existing target. - Why might a directory created with
mkdir -m 700end up with different actual permissions? The process'sumaskis applied on top of the requested mode, which can remove additional permission bits from what-mspecified. - How would you idempotently ensure a directory tree exists in a deployment script?
Use
mkdir -p path/to/dir, which succeeds whether or not the directories already exist, unlike plainmkdir.
Frequently Asked Questions
What does mkdir -p actually do?
It creates any missing parent directories along the given path, and — unlike plain mkdir — it does not error out if the final directory already exists. Without -p, mkdir fails if any parent directory in the path is missing.
How do I create a directory with specific permissions in one step?
Use mkdir -m 700 dirname to set the mode at creation time, rather than creating it and then running a separate chmod. Note that the process's umask can still restrict bits even with -m.
Why did mkdir fail with 'No such file or directory'?
This happens when you try to create a nested path (e.g. mkdir a/b/c) and one of the parent directories (a or a/b) doesn't exist yet. Add -p to create the whole chain of missing parents automatically.
Cheat sheet
Download a quick-reference cheat sheet for mkdir.