How do I create a directory, and any missing parent directories?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a directory and all missing parent directories is mostly about using the idempotent form of the tool you already have. The wrong approach is usually not "the command is unknown". The wrong approach is trying to pre-check every path segment manually instead of using the built-in parent-creation option.
On Unix-Like Systems, Use mkdir -p
On Linux and macOS, the standard command is:
The -p flag does two useful things:
- creates any missing parent directories
- does not fail if the target directory already exists
Without -p, mkdir expects the parent chain to exist already:
That is why mkdir -p is the normal answer for scripts, setup tasks, and deployment hooks.
On Windows, the Defaults Differ
In Windows Command Prompt, mkdir can create nested directories directly:
In PowerShell, a common explicit form is:
-Force is useful when you want repeated runs not to fail if the directory is already there.
In Python, Prefer pathlib
For application code and automation scripts, pathlib is usually the clearest API.
The two key arguments are:
- '
parents=Trueto create missing parents' - '
exist_ok=Trueto make repeated execution safe'
The older os.makedirs form is still fine too:
Idempotency Matters More Than Most People Expect
Directory creation often lives inside setup scripts, CI jobs, deployment steps, or application startup hooks. Those environments get rerun constantly. Code that works only the first time is usually the real bug.
That is why patterns like this are better than manual existence checks:
You do not need:
The tool already knows how to handle the normal case.
Most Real Failures Are Permission Problems
If the command still fails, the issue is often not the syntax. It is usually one of:
- no write permission on a parent directory
- an existing path segment that is a file instead of a directory
- a path variable that expanded to the wrong location
That means the best next step is often diagnostics, not another variation of mkdir.
Understanding the parent path state is usually more useful than guessing.
Validate Dynamic Paths
If the path comes from configuration, user input, or environment variables, validate it before creating directories. This is especially important in deployment and data-ingestion tooling.
The goal is not just to create directories. The goal is to create them in the right place.
Common Pitfalls
- Forgetting the parent-creation option and then manually trying to create each segment.
- Writing brittle pre-checks instead of using idempotent options like
-porexist_ok=True. - Assuming a failure is a syntax issue when it is actually a permissions or path problem.
- Hardcoding path separators in code that should stay cross-platform.
- Creating directories from unvalidated external input.
Summary
- Use
mkdir -pon Unix-like systems to create parents safely. - Use Windows
mkdiror PowerShellNew-Item -Forcefor similar behavior. - In Python,
Path.mkdir(parents=True, exist_ok=True)is the clearest programmatic form. - Prefer idempotent directory creation over manual existence checks.
- When creation fails, inspect permissions and path correctness before changing the command.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.