C.W.K.
Stream
Lesson 07 of 12 · published

mkdir, rmdir, rm — Creating and Removing

~12 min · mkdir, rmdir, rm, trash

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Make

mkdir foo creates one directory. mkdir -p a/b/c creates the whole chain at once and silently succeeds if anything already exists — perfect for scripts. mkdir -m 700 secret sets the permissions atomically (we'll see chmod soon).

Empty-only remove

rmdir foo removes an empty directory. If the dir has anything inside, it refuses. That's the safety. Day-to-day, you almost always want rm -r instead — but rmdir is the right tool when you want the safety net.

Remove for real

  • rm file — delete one file. No trash, no undo.
  • rm -i file — interactive, asks before each.
  • rm -r dir/ — recurse. Deletes everything.
  • rm -f file — force. Doesn't complain if missing, doesn't ask about read-only.
  • rm -rf dir/ — the nuclear combo. Use carefully.

There is no Recycle Bin. rm means gone unless you have a backup. macOS has the trash command via Homebrew (brew install trash) which puts files in the GUI Trash for real undo.

Patterns to fear

rm -rf $VAR/* — if $VAR is empty, this becomes rm -rf /* on bash. Always quote: rm -rf "$VAR/"* and verify $VAR is set first. The Steam Linux installer once deleted users' homes because of this exact bug.

Code

Create + remove deeply·bash
mkdir -p ~/work/2026/may/notes
ls ~/work/2026/may/
rmdir ~/work/2026/may/notes      # only if empty
rm -r ~/work                     # everything under work
Safer rm via trash·bash
brew install trash
trash ~/Downloads/old.pdf        # goes to macOS Trash
# Recover from Finder, or empty for real later

External links

Exercise

Create a deep tree: mkdir -p ~/sandbox/a/b/c. List it: tree ~/sandbox. Delete only the leaf with rmdir ~/sandbox/a/b/c (it works because it's empty). Then nuke the rest with rm -rf ~/sandbox. Install trash and redo with trash ~/sandbox to feel the difference.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.