Linux commands
directory structure
command line
file system
mkdir

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.

Browse interview questions

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:

bash
mkdir -p /var/app/data/archive

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:

bash
mkdir /var/app/data/archive
# fails if /var/app/data does not already exist

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:

cmd
mkdir C:\logs\daily\2026\03\11

In PowerShell, a common explicit form is:

powershell
New-Item -ItemType Directory -Path "C:\logs\daily\2026\03\11" -Force

-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.

python
1from pathlib import Path
2
3path = Path("data") / "exports" / "daily"
4path.mkdir(parents=True, exist_ok=True)
5
6print(path.resolve())

The two key arguments are:

  • 'parents=True to create missing parents'
  • 'exist_ok=True to make repeated execution safe'

The older os.makedirs form is still fine too:

python
import os

os.makedirs("data/exports/daily", exist_ok=True)

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:

bash
mkdir -p ./build/cache
mkdir -p ./build/output
mkdir -p ./logs

You do not need:

bash
1# unnecessarily verbose
2if [ ! -d ./logs ]; then
3  mkdir ./logs
4fi

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.

bash
whoami
ls -ld /var /var/app /var/app/data

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.

python
1from pathlib import Path
2
3root = Path("/srv/app").resolve()
4requested = (root / "uploads" / "2026" / "03").resolve()
5
6if root not in requested.parents and requested != root:
7    raise ValueError("path escaped expected root")
8
9requested.mkdir(parents=True, exist_ok=True)

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 -p or exist_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 -p on Unix-like systems to create parents safely.
  • Use Windows mkdir or PowerShell New-Item -Force for 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions