Create nested directories with mkdir -p

Build a whole folder path in one go instead of cd-ing and mkdir-ing level by level.

LinuxFilesdirectoryLow risk
Run mkdir -p path/to/deep/folder — every missing level is created in one shot.

01Command

Command
$ mkdir -p ~/projects/2026/sep/photos && mkdir -p /var/www/site/{css,js,img}

02What it does

Without -p, mkdir fails if any parent directory is missing. With -p it creates the entire chain and stays silent if directories already exist, so the command is safe to re-run in scripts. Brace expansion like {css,js,img} creates several sibling folders at once.

03Risk

RiskLow risk
!Warning: mkdir -p will happily create folders in the wrong place if you mistype the path (e.g. relative to the current directory). Double-check absolute paths starting with / before running, since fixing a misplaced tree means moving or deleting it afterwards.

04Undo

Undo
$ rmdir ~/projects/2026/sep/photos (only removes if empty)

05Sources

  • mkdir(1) — Linux manual page
    man7.org · community · 2026-09-17
    Evidence: mkdir -p creates the named directories including any missing parent directories, and does not complain if they already exist.

Last verified: 2026-09-18 · Category: Files & Folders

06Related solutions