Skip to main content

How to Create a Folder in the Terminal with mkdir

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Every project starts as a folder, and the terminal makes one with a single command: mkdir on macOS and Linux, and New-Item in PowerShell. One extra option creates a whole path of folders at once, however many of them are missing.

Syntax​

On macOS and Linux:

Syntax
mkdir [-p] <folder>
PartWhat it does
mkdirShort for "make directory". Creates the folder.
-pOptional. Also creates every missing folder in the path, and stays quiet when the folder already exists.
<folder>The folder's name, or a path such as projects/website. A name on its own is created inside the folder you're in. List several to create them all.

In PowerShell:

Syntax
New-Item -ItemType Directory -Path <folder>
PartWhat it does
New-ItemCreates a new item, such as a file or a folder.
-ItemType DirectoryMakes the new item a folder.
-Path <folder>The folder's name or path. New-Item creates any missing folders in the path by itself.

The Example​

The example creates a notes folder, tries to create it a second time, and then creates images inside website inside projects, three folders deep, with one command.

Step 1: Create a Folder​

Terminal
mkdir notes
ls
Output
notes

mkdir prints nothing when it works, so ls lists the folder's contents to show the new folder is there.

Step 2: Create a Folder That Already Exists​

Run the same command again:

Terminal
mkdir notes
Output
mkdir: notes: File exists

The command refuses, and the folder and everything in it stay as they were. A folder is never emptied or replaced by creating it again.

Step 3: Create Nested Folders in One Command​

Terminal
mkdir -p projects/website/images
ls -R projects
Output
website

projects/website:
images

projects/website/images:

-p created projects, then website inside it, then images inside that. ls -R lists a folder and every folder inside it.

A zsh terminal where mkdir notes creates a folder that ls then lists, a second mkdir notes prints mkdir: notes: File exists, and mkdir -p projects/website/images creates three nested folders that ls -R shows.

Common Mistakes​

MistakeWhat happensDo this instead
A name with a space and no quotes: mkdir my notesTwo folders, my and notesQuote it: mkdir "my notes"
A path whose parent folders don't exist, without -pmkdir: projects/website: No such file or directory, and nothing is createdAdd -p
Creating the folder somewhere you didn't mean toIt lands in the folder you're inCheck where you are with pwd first

Troubleshooting​

SymptomLikely causeFix
mkdir: notes: File existsThe folder is already thereUse it as it is, or use mkdir -p, which skips existing folders without a message
mkdir: projects/website: No such file or directoryA folder in the path doesn't exist yetmkdir -p projects/website/images
New-Item: An item with the specified name … already exists.The folder is already thereUse it as it is

FAQ​

Where does the new folder go?​

Into the folder you're in, unless the path says otherwise. How to Move into a Folder in the Terminal with cd shows how to see where you are and how to move.

Can I create several folders with one command?​

Yes. mkdir notes photos music creates all three.

Does mkdir work in PowerShell too?​

Yes, with a difference. On Windows, PowerShell's mkdir creates the folder with New-Item. On macOS and Linux, PowerShell leaves mkdir to the system's own command, so it behaves exactly like the first tab. New-Item -ItemType Directory works the same everywhere.

Conclusion​

mkdir <folder> creates a folder on macOS and Linux, mkdir -p creates every missing folder in a path, and New-Item -ItemType Directory -Path <folder> does both in PowerShell. Creating a folder that already exists changes nothing.

Additional Resources​