mkdir -p Explained: Linux Command Examples, Use Cases & Best Practices

Creating folders in Linux can feel like building a tiny city. One street here. One house there. Then suddenly you need a whole neighborhood. That is where mkdir -p becomes your friendly construction crew.

TLDR: The command mkdir -p creates parent folders as needed, and it does not complain if the folder already exists. For example, mkdir -p projects/2026/reports creates all three folders in one go. In a small team setup, this can cut setup steps by more than 50% when creating repeatable project structures. It is simple, safe, and great for scripts.

What Is mkdir?

The Linux command mkdir means make directory. A directory is just a folder. So, when you run:

mkdir photos

Linux creates a folder named photos in your current location.

Easy, right?

But there is a catch. If you try to create a folder inside a folder that does not exist, regular mkdir gets grumpy.

mkdir work/clientA/invoices

If work or clientA does not exist, you will see an error like this:

mkdir: cannot create directory ‘work/clientA/invoices’: No such file or directory

Linux is saying, “I cannot put a box inside a box that does not exist.” Fair point.

So, What Does -p Do?

The -p option means parents. It tells mkdir to create parent directories too.

Run this:

mkdir -p work/clientA/invoices

Now Linux creates:

  • work
  • work/clientA
  • work/clientA/invoices

All in one command.

That is the magic. No drama. No error. Just folders.

Why Is mkdir -p So Useful?

mkdir -p is useful because it is safe, fast, and script friendly.

Here is the big benefit. If the folder already exists, mkdir -p does not fail.

Try this:

mkdir -p backups

Then run it again:

mkdir -p backups

No error. No complaint. Linux just shrugs and moves on.

This is great in scripts. A script may run many times. You do not want it to crash just because a folder already exists.

Basic Syntax

The basic syntax is short:

mkdir -p path/to/folder

Think of the path as a trail. If parts of the trail are missing, -p builds them.

Example:

mkdir -p school/math/homework

This creates the full folder path.

Simple Examples

1. Create One Folder

mkdir -p notes

This creates a folder named notes. If it already exists, nothing bad happens.

2. Create Nested Folders

mkdir -p business/reports/january

This creates a full chain of folders. Very handy.

3. Create Multiple Folders

mkdir -p app/css app/js app/images

This creates three folders inside app. If app does not exist, it gets created too.

4. Create a Date Based Backup Folder

mkdir -p backups/2026/08/04

This is common in backup jobs. It keeps files tidy by year, month, and day.

A Real Use Case: Project Setup

Imagine you build websites. Each new website needs the same folders:

  • css for styles
  • js for scripts
  • images for pictures
  • docs for notes

You could create them one by one. That is boring. Your keyboard deserves better.

Use this:

mkdir -p mysite/assets/css mysite/assets/js mysite/assets/images mysite/docs

Boom. Your project structure is ready.

If a developer creates 10 projects a week, and each project needs 8 folders, that is 80 folder actions. With mkdir -p, this can drop to around 10 commands. That is less typing and fewer chances to make silly mistakes.

Image not found in postmeta

Using mkdir -p in Scripts

This is where mkdir -p really shines.

Here is a tiny Bash script:

#!/bin/bash

mkdir -p logs
mkdir -p output/reports
mkdir -p tmp/cache

echo "Folders are ready!"

This script prepares folders before doing other work. It can run once. It can run 100 times. It still works.

That is why system admins and developers love it. It makes scripts more reliable.

Using Permissions with mkdir -p

You can also set permissions while creating folders.

mkdir -p -m 755 public/files

The -m option sets the mode, or permission level.

In simple terms:

  • 755 means the owner can read, write, and enter.
  • Others can read and enter.
  • Others cannot write.

This is common for web folders.

Be careful, though. Permissions matter. Too open is risky. Too strict can break things.

Common Mistakes

Forgetting the Space

This is wrong:

mkdir-p folder

Linux sees mkdir-p as a different command. It will not work.

This is right:

mkdir -p folder

Using Weird Paths

Be careful with spaces in folder names.

This:

mkdir -p my project/files

creates two things: my and project/files. Oops.

Use quotes instead:

mkdir -p "my project/files"

Running in the Wrong Folder

Always check where you are.

pwd

This prints your current directory. It is like asking Linux, “Where am I?”

Best Practices

Follow these simple tips:

  • Use -p in scripts. It prevents errors when folders already exist.
  • Use clear folder names. Future you will be thankful.
  • Quote paths with spaces. Better yet, avoid spaces in script paths.
  • Check your current directory. Use pwd before creating big structures.
  • Do not use sudo unless needed. Creating folders as root can cause permission headaches.
  • Test commands first. Especially inside automation scripts.
Image not found in postmeta

mkdir -p vs Regular mkdir

Here is the simple difference:

  • mkdir folder creates one folder.
  • mkdir -p a/b/c creates the full path.
  • mkdir folder fails if the folder exists.
  • mkdir -p folder does not fail if the folder exists.

So, should you always use mkdir -p?

Not always. If you want to know when a folder already exists, regular mkdir can be useful. It gives an error. That error may be helpful.

But for setup tasks, automation, and nested folders, mkdir -p is usually the better tool.

Fun Mental Model

Think of regular mkdir as a picky builder.

It says, “I will build this room, but only if the hallway already exists.”

Now think of mkdir -p as a cheerful builder with a toolbox.

It says, “No hallway? No problem. I will build that too.”

That is why it feels so smooth.

Quick Command Cheat Sheet

  • mkdir -p folder creates a folder safely.
  • mkdir -p one/two/three creates nested folders.
  • mkdir -p a b c creates multiple folders.
  • mkdir -p "folder with spaces" handles spaces.
  • mkdir -p -m 755 public creates a folder with permissions.

Final Thoughts

mkdir -p is small, but powerful. It saves time. It avoids annoying errors. It makes scripts cleaner. It also helps you build neat folder structures without clicking around like a lost squirrel.

If you use Linux often, learn this command well. It will show up in projects, backups, deployments, logs, and scripts. And once you get used to it, regular folder creation will feel slow.

So next time you need a folder path, do not build it one brick at a time. Use mkdir -p. Let Linux do the heavy lifting.

Thanks for Reading

Enjoyed this post? Share it with your networks.