>_cmd.script

mkdir

Create new directories

Directories

By CMD Script Team · 4 min read · Last updated

SYNTAX
mkdir [OPTIONS] DIRECTORY...

Options

Command options and flags
FlagDescription
-pCreate parent directories as needed; don't error if the directory already exists
-m MODESet the permission mode of the new directory at creation time
-vPrint a message for each directory created
--parentsLong 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 location
  • mkdir docs tests build — create several sibling directories in one command
  • mkdir -p project/src/components — create a nested path, creating any missing intermediate directories
  • mkdir -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 build won't error even if build already exists, unlike plain mkdir build.
  • Combine with find to bulk-create a mirrored directory structure: find source -type d -exec mkdir -p dest/{} \; (conceptually; typically done with rsync --dirs-only/cp -r --parents in practice).
mkdir -p app/{src,tests,docs} && mkdir -m 750 app/config

Common mistakes

  • Forgetting -p when creating a nested path and getting "No such file or directory" because an intermediate parent directory doesn't exist yet.
  • Assuming -m MODE is the final permission — the process's umask can still mask out bits from the requested mode, so the resulting permissions may be more restrictive than expected.
  • Running plain mkdir dirname in a script expecting it to be a no-op if the directory already exists — it errors out instead; use -p for idempotent scripts.
  • Confusing mkdir's inability to create files with the need for a separate touch or redirect — mkdir only ever creates directories, never regular files.

Tips

  • Use mkdir -p by 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 dirname after using -m, since umask can still restrict the actual bits applied.

Best practices

  • Prefer mkdir -p over plain mkdir in 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 -m for 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 -p so 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 -p do differently from plain mkdir? -p creates any missing parent directories along the path and doesn't error if the target directory already exists; plain mkdir requires all parents to exist and fails on an existing target.
  • Why might a directory created with mkdir -m 700 end up with different actual permissions? The process's umask is applied on top of the requested mode, which can remove additional permission bits from what -m specified.
  • 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 plain mkdir.

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.