Creating folders
You’ve been moving around folders someone else made for you. Now you make your own.
The command is mkdir — make directory. (“Directory” is just the old Unix word for folder.
You’ll see both words used interchangeably forever.)
The basic case
Pick a safe spot to play in — cd ~ to go home first, then:
mkdir sandbox
Run ls. There’s a new folder called sandbox. Step into it:
cd sandbox
You can keep making folders inside:
mkdir notes
mkdir photos
mkdir code
Now ls shows three folders. You just built a tiny filesystem.
The case that surprises everyone once
What if you want a folder inside another folder that doesn’t exist yet? Try:
mkdir projects/website/src
The terminal complains:
mkdir: cannot create directory 'projects/website/src': No such file or directory
By default, mkdir only makes the last folder in the path. It refuses to invent the missing
middle folders for you. (Which is, when you think about it, a fair safety check — typos
shouldn’t quietly create whole new folder trees.)
The fix: -p
Add the -p flag (short for parents):
mkdir -p projects/website/src
Now the terminal creates every folder in the path that doesn’t already exist. projects,
then website inside it, then src inside that. Done in one shot.
-p has a second handy property: it doesn’t complain if the folder already exists. Plain
mkdir foo errors if foo is already there; mkdir -p foo silently succeeds. That makes it
the safer choice in scripts you run more than once.
-p is one of those tiny flags you’ll type ten thousand times. Memorize it.
Many at once
You can give mkdir multiple names at once. They’re created side by side:
mkdir drafts final archive
Three folders in one keystroke. This works for almost any “make something” command in the terminal — pass several names, get several results.
What you’ve learned
mkdir <name>— make one folder.mkdir -p <path>— make a whole nested tree, no errors if parts already exist.mkdir a b c— make several side-by-side folders at once.
Next: making files.