Coding⏱️ 3 min read📅 2026-06-03

How to Fix: How do I create a directory, and any missing parent directories?

Create a directory and its parent directories using the mkdir -p command in Bash.

Quick Answer: Use the mkdir -p command to create a directory and any missing parent directories, e.g., mkdir -p /path/to/nested/directory

Creating a directory with its parent directories can be frustrating when working with file systems, especially in Unix-based operating systems like Linux and macOS. This issue affects users who need to organize their files and directories efficiently.

This problem is particularly frustrating because it requires manual intervention to create the missing parent directories, which can lead to tedious and time-consuming work.

🔍 Why This Happens

  • The primary reason for this issue lies in the way Unix-based operating systems handle directory creation. When a user attempts to create a directory using the `mkdir` command without specifying the `-p` option, the system will only create the specified directory if it does not already exist.
  • Another possible cause is that the user may be working on a file system with limited permissions or access rights, restricting the ability to create directories at certain paths.

✅ Best Solutions to Fix It

Using the `mkdir -p` Command

  1. Step 1: To create a directory and its parent directories using the `mkdir -p` command, first navigate to the desired directory path in the terminal. Then, use the following syntax: `mkdir -p /path/to/nested/directory`. This will recursively create all parent directories if they do not already exist.
  2. Step 2: For example, to create a directory at `/Users/username/Documents/Reports`, you would run the command `mkdir -p /Users/username/Documents/Reports` in the terminal. The system will automatically create the `/Users/username/Documents/` and `/Users/username/Documents/Reports` directories if they do not exist.

Using Shell Scripting

  1. Step 1: Alternatively, you can use shell scripting to create directories with their parent directories. Create a new text file with a `.sh` extension (e.g., `create_directories.sh`) and paste the following command: `mkdir -p /path/to/nested/directory`. Save the file and make it executable using the `chmod +x create_directories.sh` command.
  2. Step 2: To execute the script, navigate to the directory containing the script and run `./create_directories.sh` in the terminal. The system will automatically create all parent directories if they do not exist.

🎯 Final Words

By following these steps, you can efficiently create a directory with its parent directories using either the `mkdir -p` command or shell scripting. Remember to always navigate to the correct directory path before running the command, and be cautious when working with file systems to avoid any potential errors.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions