mkdir & rmdir
mkdir (make directory) creates directories and rmdir (remove directory) deletes empty ones. Together with -p they let you create or tidy up whole folder structures in one command.
Creating directories
mkdir photos
mkdir music videos
ls -Fmusic/
photos/
videos/
Each argument becomes a new directory. Trying to create one that already exists, or one inside a parent that doesn't exist, fails:
mkdir photos
mkdir photos
mkdir 2026/januarymkdir: cannot create directory ‘photos’: File exists
mkdir: cannot create directory ‘2026/january’: No such file or directory
-p: parents and no errors
mkdir -p creates every missing parent directory along the path and stays quiet if the directory already exists. That makes it the safe choice for scripts, which often run more than once:
mkdir -p 2026/january/receipts
mkdir -p 2026/january/receipts
echo "exit status: $?"
find 2026exit status: 0
2026
2026/january
2026/january/receipts
Creating many directories at once
Combine -p with brace expansion ({a,b,c} and {1..3}) to build a whole tree in one line. Bash expands the braces into separate arguments before mkdir runs:
mkdir -p webapp/{src/{components,pages},public,tests}
mkdir -p logs/2026-{01..03}
echo webapp/{src,tests}
find webapp logs | sortwebapp/src webapp/tests
logs
logs/2026-01
logs/2026-02
logs/2026-03
webapp
webapp/public
webapp/src
webapp/src/components
webapp/src/pages
webapp/tests
-v (verbose) makes mkdir report each directory it creates, which is handy in setup scripts:
mkdir -pv data/{raw,clean}mkdir: created directory 'data'
mkdir: created directory 'data/raw'
mkdir: created directory 'data/clean'
Tip:
mkdir -m 700 secretscreates a directory with specific permissions (here: only you can enter it). Permissions are covered in the chmod lesson.
Removing empty directories
rmdir only deletes directories that are empty. That is a safety feature: you can't lose files with it.
mkdir -p old/empty-folder
touch old/keep.txt
rmdir old/empty-folder
rmdir old
ls oldrmdir: failed to remove 'old': Directory not empty
keep.txt
The second rmdir refuses because old still contains keep.txt. To delete a directory and everything inside it you need rm -r (next lesson), which deserves much more care.
rmdir -p
rmdir -p a/b/c removes c, then b, then a, stopping as soon as one of them is not empty:
mkdir -p archive/2025/q4
rmdir -pv archive/2025/q4
lsrmdir: removing directory, 'archive/2025/q4'
rmdir: removing directory, 'archive/2025'
rmdir: removing directory, 'archive'
Naming tips
| Do | Avoid |
|---|---|
monthly-reports, monthly_reports |
Monthly Reports (spaces need quoting everywhere) |
| lowercase names | mixing Docs and docs (two different directories on Linux) |
ISO dates 2026-03 |
03-2026 (doesn't sort chronologically) |
Test yourself
Which command creates a/b/c even if a doesn't exist yet?
- mkdir a/b/c
- mkdir -p a/b/c
- mkdir -r a/b/c
- rmdir -p a/b/c
What happens when you run rmdir on a directory that contains a file?
- The directory and file are deleted
- Only the file is deleted
- It fails with
Directory not empty - It asks for confirmation
What does mkdir -p site/{css,js} create?
- A directory literally named
{css,js} site/cssandsite/js- Only
site cssandjsin the current directory