Where to start First steps track · 15 stages
Linux Labs

mkdir — creating directories

3 min Linux · coreutils

Creating directories: -p (with parents), -m (permissions right away), brace expansion, rmdir.

Cheat sheet

4 commands Printable version
  • mkdir -p a/b/c a chain of directories
  • mkdir -p site/{css,js,img} several at once
  • mkdir -m 700 ~/.secrets with permissions right away
  • rmdir empty remove an empty one

Overview

mkdir (make directory) creates directories. An empty directory is removed with rmdir, a non-empty one with rm -r (careful, see files-basics).

The key option for beginners is -p: it creates the whole chain of parent directories and doesn't complain if the directory already exists.

Options

3
bash
mkdir reports                     # one directory in the current one
mkdir a b c                       # several at once
mkdir -p proj/src/api             # -p: create the WHOLE chain (proj and proj/src too)
mkdir -p logs                     # -p also doesn't fail if logs already exists (handy in scripts)
mkdir -m 700 ~/.secrets           # -m: set permissions right away (here — owner only)
mkdir -v new                      # -v: print what was created (verbose)
mkdir -p site/{css,js,img}        # bash brace expansion → site/css site/js site/img
mkdir -p data/{2024,2025}/{01,02} # combinations: data/2024/01 … data/2025/02

Main options:

Option What it does
-p, --parents create parent directories along the path; not an error if it already exists
-m, --mode=MODE set permissions right at creation time (instead of umask)
-v, --verbose print the name of every directory created
Option What it does Example
-p --parents Create the full chain of parent directories; not an error if it already exists. mkdir -p proj/src/api
-m --mode ПРАВА Set the mode of the created directory immediately, bypassing umask. mkdir -m 700 ~/.secrets
-v --verbose Print the name of each created directory. mkdir -v new

Nothing found. Try a shorter query.

Recipes

1

rmdir

bash
rmdir empty                       # remove an EMPTY directory
rmdir -p a/b/c                    # remove c, b, a — if each one becomes empty
rmdir dir 2>/dev/null || rm -r dir  # if not empty — remove it recursively

rmdir deliberately only removes empty directories — that's a safety net. For a non-empty one you need rm -r (irreversible, double-check the path).

Troubleshooting

4

mkdir: cannot create directory 'x': File exists

The directory already exists. In scripts, use mkdir -p — it skips silently.

mkdir: cannot create directory 'a/b/c': No such file or directory

The intermediate directories (a, a/b) don't exist. Add -p.

Permission denied

No w+x permission on the parent directory. Check ls -ld . and the permissions — see chmod-chown.

The permissions aren't what -m specified

-m sets the resulting directory's permissions directly (umask doesn't apply to it), but the intermediate directories created by -p still get the regular umask.

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
  • cd — Moving around the filesystem: cd, cd -, cd ~, cd .., pushd/popd, CDPATH.
  • chmod-chown — Changing permissions (chmod) and owner/group (chown) — numeric and symbolic syntax.
Loading…