Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux mkdir command

Reference for the Linux mkdir command: create directories, nested paths with -p, set permissions with -m, brace expansion and common errors.

mkdir creates one or more directories. By default every parent of the new directory must already exist; the -p option creates missing parents too and, as a bonus, does not complain when the directory is already there, which makes it ideal for scripts that must be safe to run twice.

New directories get the mode 0777 minus your umask (typically 0755 or 0775). The -m option sets the mode directly.

Terminal
mkdir [OPTION]... DIRECTORY...

Try it

Bash Single, nested and multiple directories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Modes and brace expansion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
mkdir [-pv] [-m MODE] DIRECTORY...
Option Description
-p, --parents Create missing parent directories; no error if the directory already exists.
-m MODE, --mode=MODE Set permissions of the new directory, in octal (750) or symbolic (u=rwx,go=rx) form.
-v, --verbose Print a message for each directory created.
-Z Set the SELinux security context to the default type (where supported).

Returns: 0 if all directories were created; non-zero if any failed. With several arguments, mkdir keeps going and reports each failure.

Common errors: "File exists" (a file or directory of that name is already there), "No such file or directory" (a parent is missing and -p was not used), "Permission denied" (no write permission on the parent), "Not a directory" (a component of the path is a regular file).

Usage notes

  • -p is idempotent, but only if the existing item is a directory. If a regular file has that name, mkdir -p still fails.
  • -m and parents: with -p, the mode applies only to the final directory. Parents created on the way get the default mode (and always have owner write and execute added, so the command can continue).
  • umask interaction: without -m, the umask removes bits from 0777. With -m, the mode is applied as given, except that a symbolic mode like g+w is relative to the default.
  • Brace expansion is done by the shell before mkdir runs: mkdir -p logs/{2024,2025}/{01..12} creates twenty-four directories in one command. Remember the braces only work in Bash, Zsh and Ksh, not in plain sh.
  • Directory names with spaces need quotes: mkdir "My Documents".
  • Names starting with a dash need --: mkdir -- -odd.
  • Temporary directories: use mktemp -d instead of inventing a name in /tmp; it avoids collisions and races.
  • Setting an owner needs another command (chown) or install -d -o user -g group -m 755 dir.
  • Race-free creation: mkdir without -p is atomic and fails if the name exists, which scripts sometimes use as a simple lock.