mkdir -p: Create Directory & Parent Directories
Learn mkdir -p equivalents to create a directory and missing parent directories in Linux/macOS, Python (os.makedirs), PowerShell (New-Item), Node.js, Java. Avoid race conditions with exist_ok=True.
How do I create a directory and any missing parent directories? I need to create a directory at a given path, and also create any missing parent directories along that path. For example, the Bash command mkdir -p /path/to/nested/directory accomplishes this. What are the equivalent methods in other systems?
Use mkdir -p /path/to/nested/directory on Unix-like systems to create a directory and any missing parent directories. Equivalent methods: in Python use os.makedirs(path, exist_ok=True) or pathlib.Path(path).mkdir(parents=True, exist_ok=True); on Windows/PowerShell use New-Item -ItemType Directory -Path (or the mkdir alias). Many runtimes and languages (Node.js, Java, Dockerfiles) provide similar “create parents” flags.
Contents
- Linux / macOS — mkdir -p
- Python — os.makedirs & pathlib (mkdir -p equivalent)
- PowerShell & Windows — New-Item / mkdir (powershell new-item directory)
- Other environments and practical tips
- Sources
- Conclusion
Linux / macOS — mkdir -p
On Unix-like systems the canonical command is:
mkdir -p /path/to/nested/directory
The -p option tells mkdir to create parent directories as needed and not to fail if the target already exists. Short and reliable. If a directory in the path already exists, mkdir -p does nothing for that segment and continues. See the POSIX/manual notes for details and examples: the mkdir manual and practical write-ups like this freeCodeCamp guide explain the behavior and common flags.
Notes:
- Permissions matter: if you don’t have write permission on a parent directory the command will fail.
- Use quoting for paths with spaces:
mkdir -p "$HOME/my dir/subdir".
Python — os.makedirs & pathlib (mkdir -p equivalent)
Python provides direct equivalents that create intermediate directories.
os.makedirs (recommended, simple):
import os
os.makedirs('/path/to/nested/directory', exist_ok=True)
exist_ok=True(Python 3.2+) prevents an exception if the directory already exists.
pathlib (modern, object-oriented):
from pathlib import Path
Path('/path/to/nested/directory').mkdir(parents=True, exist_ok=True)
parents=Trueis equivalent to-pandexist_ok=Truesuppresses errors if the directory already exists.
For older Python versions (pre-3.2) use the try/except pattern to handle races safely:
import os, errno
try:
os.makedirs('/path/to/nested/directory')
except OSError as e:
if e.errno != errno.EEXIST or not os.path.isdir('/path/to/nested/directory'):
raise
These patterns are discussed in community Q&A and tutorials — see this StackOverflow thread on mkdir -p behavior in Python and general how-tos at TutorialsPoint and GeeksforGeeks.
Why prefer exist_ok=True/parents=True? They avoid a check-then-create race where another process creates the directory between your existence check and the creation call.
PowerShell & Windows — New-Item / mkdir (powershell new-item directory)
PowerShell exposes New-Item (and an alias mkdir) to create directories:
New-Item -ItemType Directory -Path "C:\path\to\nested\directory"
# or using the mkdir alias:
mkdir "C:\path\to\nested\directory"
PowerShell will create the specified directory path; use -Force to suppress errors in some edge cases or to overwrite read-only attributes where applicable:
New-Item -ItemType Directory -Path "C:\path\to\nested\directory" -Force
Refer to the official docs for syntax and examples: New-Item (Microsoft.PowerShell.Management) and a concise reference at SS64.
Note: In classic cmd.exe, mkdir / md will also create nested directories when given a multi-segment path (for example md C:\a\b\c).
Other environments and practical tips
- Node.js (since v10.12.0): use the
recursiveoption.
const fs = require('fs');
fs.mkdirSync('/path/to/nested/dir', { recursive: true });
// async version:
fs.mkdir('/path/to/nested/dir', { recursive: true }, err => { /* ... */ });
Before recursive existed many projects used the mkdirp package.
- Java (NIO):
import java.nio.file.*;
Files.createDirectories(Paths.get("/path/to/nested/directory"));
createDirectories creates all nonexistent parents.
- Dockerfile / shell in containers:
RUN mkdir -p /app/logs
or run the same mkdir -p inside container shells.
Practical tips:
- Prefer the language/native API that creates parents atomically (e.g.,
exist_ok=True,recursive:true,createDirectories) rather than checking + creating, to avoid race conditions. - Watch permissions and umask on Unix; a process without write permission on a parent will get an error.
- Normalize paths (use
PathAPIs) to avoid differences in separators and trailing slashes. - If you need special ownership or modes, create parents then set the attributes explicitly where the API doesn’t accept mode arguments.
Sources
- How do I create intermediate directories with mkdir -p in Python?
- mkdir manual page (linuxcommand.org)
- mkdir Command in Linux – How to Create a New Directory/Folder (freeCodeCamp)
- New-Item (Microsoft.PowerShell.Management) - PowerShell
- How do I create a directory if it doesn’t exist using Python? (TutorialsPoint)
- How to create a directory in Python? (GeeksforGeeks)
- mkdir - Shell Scripting Tutorial (shellscript.sh)
- New-Item - PowerShell (SS64)
Conclusion
For straightforward, reliable creation of a directory plus any missing parents use the native “create-parents” option for your environment: mkdir -p on Unix, os.makedirs(..., exist_ok=True) or Path(...).mkdir(parents=True, exist_ok=True) in Python, New-Item -ItemType Directory -Path (or mkdir) in PowerShell, and similar flags in Node.js or Java. Pick the API that handles existence and races for you (exist_ok/recursive/createDirectories) and you’ll get the same behavior as mkdir -p.