Recursively Create Directory Structure Command Line
The command-line method for recursively creating a directory structure is the mkdir -p command on Linux and macOS, or mkdir with the /p flag in Windows Command Prompt and PowerShell. You provide the full path of nested folders, and the command creates any missing parent directories in one go, without erroring if they already exist.
What Recursive Directory Creation Means
Recursive directory creation means telling the operating system to create a folder, and if that folder’s parent folder doesn’t exist, to create the parent too, and so on up the chain. A single command like mkdir -p projects/2025/reports/january will create projects, then projects/2025, then projects/2025/reports, and finally projects/2025/reports/january. Without the recursive flag, mkdir would fail with an error like “No such file or directory” if any intermediate folder is missing.
The concept is standard across all major operating systems, though the exact flag differs. The -p flag (short for “parents”) is used on Unix-like systems, while Windows uses /p in both Command Prompt and PowerShell. Some tools like install -d on Linux also create directories recursively, and many scripting languages have their own recursive creation functions, such as os.makedirs() in Python.
How to Use mkdir -p on Linux and macOS
On Linux and macOS, the command is simple:
- Open a terminal window.
- Type
mkdir -p path/to/nested/directoryand press Enter. - Verify with
ls -Rorfindto see the full tree.
For example, to create a project structure for a website:
mkdir -p website/css website/js website/img
This creates three separate directory trees in one command. You can also use curly braces to generate multiple paths with a single command:
mkdir -p project/{src,dist,docs}/{css,js}
This expands to create project/src/css, project/src/js, project/dist/css, and so on. The shell expands the braces before mkdir runs, so it’s a convenient way to create many nested folders at once.
How to Use mkdir /p on Windows
Windows Command Prompt and PowerShell both support recursive directory creation with the /p flag:
mkdir /p projects\2025\reports\january
In PowerShell, you can also use the New-Item cmdlet with the -ItemType Directory parameter, which creates parent directories by default:
New-Item -ItemType Directory -Force -Path projects\2025\reports\january
The -Force parameter ensures no error if the directory already exists. In modern Windows 10 and 11, mkdir in PowerShell is an alias for New-Item, so both approaches work identically.
Choosing Between mkdir -p and Other Methods
While mkdir -p is the standard, there are alternatives depending on your needs:
- Plain
mkdir(no flag) is useful when you want to create only a single directory and intentionally fail if the parent doesn’t exist, which can be a safety check in scripts. install -don Linux creates directories and sets permissions, useful for build scripts.mkdir -v(verbose) prints each directory as it’s created, helpful for debugging.- File managers (like Windows Explorer or Finder) create parent folders automatically when you create a nested folder, but only one level at a time via the GUI.
For most command-line use, mkdir -p is the right tool because it’s idempotent: running it multiple times doesn’t cause errors, which is valuable in scripts that may run repeatedly.
Common Mistakes and How to Avoid Them
One frequent mistake is forgetting the flag and getting an error when the parent directory doesn’t exist. Another is using backslashes on Unix or forward slashes on Windows; while modern systems often accept both, it’s safer to use the native separator. On Windows, backslashes are standard, but forward slashes also work in most contexts.
Another issue is with spaces in directory names. Always quote the path or escape spaces with a backslash on Unix:
mkdir -p "My Project/docs"
Without quotes, the shell splits the path into multiple arguments and creates multiple directories. Also, be careful with relative vs. absolute paths. mkdir -p /tmp/a/b creates directories from the root, while mkdir -p a/b creates them relative to the current working directory.
Finally, when using braces expansion, ensure there are no spaces inside the braces, or the shell won’t expand them as expected.
Safety and Permissions
Creating directories is generally safe, but there are a few considerations:
- Permissions: On Unix, directories are created with permissions determined by the umask, typically 755 (rwxr-xr-x). You can change this with
chmodafter creation. - Existing directories:
mkdir -pdoes not overwrite or modify existing directories; it simply leaves them as they are. - Symbolic links: If a path component is a symlink to a directory,
mkdir -pwill follow it and create directories inside the target, which can be unexpected. - Root privileges: Creating directories in system locations like
/usrmay requiresudo; otherwise, you’ll get a permission denied error.
For scripting, always check the exit code of mkdir to ensure it succeeded. In a shell script, you can use set -e to exit on any error, which is a good practice.
Alternatives and Related Commands
Besides mkdir -p, there are other ways to create directory structures:
treecommand (if installed) can display the directory structure after creation.findwith-type dcan list directories.- Python’s
os.makedirsis the equivalent in scripts:os.makedirs('a/b/c', exist_ok=True). - PowerShell’s
New-Itemas mentioned, which is more flexible for setting properties. - Bash’s
mkdir -pis also available in most Unix-like environments, including Git Bash on Windows.
For creating a full project skeleton, many developers use scaffolding tools like npm init or cookiecutter, but those are overkill for a simple directory tree.
Frequently Asked Questions
What does the -p flag do in mkdir?
The -p flag stands for “parents.” It tells mkdir to create any missing parent directories along the path. Without it, mkdir only creates the final directory if its parent exists.
Does mkdir -p overwrite existing directories?
No. If the directory already exists, mkdir -p does nothing and returns a success status. It will not modify the contents or permissions of an existing directory.
How do I create multiple nested directories at once?
You can pass multiple paths to mkdir -p, or use brace expansion in Bash to generate multiple paths. For example, mkdir -p project/{src,docs}/{js,css} creates four directories.
Why do I get “No such file or directory” when using mkdir?
This error occurs when you use mkdir without the -p flag and the parent directory doesn’t exist. Add -p to create the entire path recursively.
Can I use mkdir -p on Windows?
Yes, Windows Command Prompt and PowerShell support mkdir /p (slash instead of dash). PowerShell also allows mkdir -p because it accepts both dash and slash for flags, but the slash form is more traditional.
What is the difference between mkdir and mkdir -p?
mkdir creates a single directory and fails if the parent doesn’t exist. mkdir -p creates the entire path, including all missing parents, and doesn’t error if the directories already exist.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.