Create directory

2 snippets across 2 stacks - Bash & Linux, Python

SHBash & Linux

Create Directories

SH · Navigation & Files
Syntax
mkdir [options] directory...
Example
mkdir -p src/components/auth
mkdir -m 750 secrets

Note -p creates the full path including any missing parent directories and will not error if the directory already exists. -m sets permissions at creation time.

PYPython

pathlib (Modern Path Handling)

PY · File I/O
Syntax
from pathlib import Path
Example
from pathlib import Path

data_dir = Path("project") / "data"
data_dir.mkdir(parents=True, exist_ok=True)

config = data_dir / "settings.json"
config.write_text('{"debug": true}', encoding="utf-8")
print(config.read_text())
print(config.suffix, config.stem)
Output
{"debug": true}
.json settings

Note pathlib is the modern replacement for os.path. The / operator joins paths. Use .resolve() for absolute paths, .exists() to check existence.

Frequently asked questions

How do you create directory?
This task is covered in 2 stacks on this page: Bash & Linux, Python. The "Create Directories" snippet in Bash & Linux uses `mkdir [options] directory...`.
Which command does the Bash & Linux example use?
The "Create Directories" snippet uses `mkdir [options] directory...`, from the Navigation & Files section of the Bash & Linux cheat sheet.
Which stacks cover "create directory" on this page?
Bash & Linux, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Create Directories": -p creates the full path including any missing parent directories and will not error if the directory already exists. -m sets permissions at creation time.