What rm does
- Deletes files or directories from the filesystem.
- No trash/recycle bin by default (CLI). Once gone, it’s (usually) gone.
rm file.txt # delete a file
rm -r mydir/ # delete a directory and its contents (recursive)
1) Everyday, safe usages
rm -i notes.txt # -i = ask before delete (good while learning)
rm -ri lab/ # ask for each file in a directory (recursive + interactive)
rm -v report.pdf # -v = verbose (shows what it deletes)
rm -rIv project/ # I = prompt once before mass deletion; v = verbose
Tip: While you’re new, prefer -i or -I and sometimes -v.
2) Common options (GNU rm on Linux)
- -i: interactive (ask before every removal).
- -I: prompt once if you’re removing >3 files or recursively.
- -r / -R / --recursive: remove directories and everything inside.
- -d: remove empty directories (like rmdir).
- -f / --force: ignore nonexistent files, never prompt. (Dangerous)
- -v: verbose — show each file as it’s removed.
- --preserve-root (default on GNU): refuse to operate on /.
- --no-preserve-root: disables the safety above (never use as a beginner).
Student-safe defaults to remember:
· For files: rm -i file
· For folders: rm -rI folder/ (prompt once) or rm -ri folder/ (prompt each)
3) Removing files vs directories
rm file.txt # ok (file)
rm -r dir/ # needed for directories
rm -d emptydir/ # delete only if empty
- If you forget -r on a directory, you’ll get: “is a directory”.
- Hidden files (start with a dot) aren’t matched by * unless you include them explicitly or enable dotglob.
4) Globs, spaces, and “weird” names
- Quote paths with spaces/special characters:
· rm "My File (final).txt"
- Filenames starting with - (dash) are treated like options. Use -- to end options:
· rm -- -strange-name.txt
- To delete many files safely (especially with spaces/newlines in names), pair with find:
· find . -name "*.tmp" -print0 | xargs -0 rm -f
5) Symlinks (shortcuts)
- rm symlink deletes the link, not the target.
- rm symlink/ (with a trailing slash) tries to treat it as a directory and will error—don’t add / when deleting a link.
6) Useful patterns you’ll actually use
# Delete logs older than 30 days (in one directory tree)
find /var/log/myapp -type f -mtime +30 -name "*.log" -delete
# Clean build artifacts
find build -type f \( -name "*.o" -o -name "*.tmp" \) -delete
# Force-remove a file you own but is write-protected (asks once):
rm -i file.txt
# Verbose recursive delete (see what’s happening):
rm -rv old_dataset/
7) Common errors & fixes
- No such file or directory: path or glob typo → use Tab completion; ls first.
- Permission denied: you may lack permissions or execute bit on the directory:
- Try sudo rm ... (if appropriate), or fix perms: chmod +w file or chmod +x dir.
- Rare: immutable files (advanced): sudo chattr -i file then rm file.
- Directory not empty: use -r or -d only for empty directories.
- “Argument list too long” when expanding huge globs: use find … -delete or find … -print0 | xargs -0 rm.
8) Safer alternatives (great for students)
- Send to trash (recoverable):
· sudo apt install trash-cli
· trash-put file.txt # move to Trash instead of permanent delete
· trash-list # see items
· trash-restore # restore interactively
- Version control (git):
· git rm file.txt # tracked file removal (recoverable via history)
9) Why rm -rf is infamous (and how to avoid disasters)
- -r = recursive, -f = force → deletes everything it can, without asking.
- A tiny typo can wipe critical data (e.g., extra space, wrong directory).
- Never run sudo rm -rf / or sudo rm -rf *.
- Good habits:
- Double-check pwd and destination before running.
- Echo first to preview:
o echo rm -rI ~/data/old_runs/*
- Use -I/-i while learning; add -v to see what’s happening.
- Prefer trash-put during class labs.
10) Mini-lab (15–20 minutes)
# Setup
mkdir -p ~/lab/rm/{empty,docs,logs}
printf "A\n" > ~/lab/rm/docs/a.txt
printf "B\n" > ~/lab/rm/docs/b.txt
printf "log\n" > ~/lab/rm/logs/app.log
touch ~/lab/rm/empty/.keep
# 1) delete a single file (interactive)
rm -i ~/lab/rm/docs/a.txt
# 2) try removing a directory without -r (should fail)
rm ~/lab/rm/docs
# 3) delete directory contents safely (prompt each)
rm -ri ~/lab/rm/docs
# 4) remove empty dir with -d
rm -d ~/lab/rm/empty # will fail if not really empty; then:
rm -d ~/lab/rm/empty # after you delete .keep
# 5) use find -delete (pattern)
find ~/lab/rm/logs -type f -name "*.log" -delete
Exam-ready bullets
- Purpose: rm removes files; -r removes directories recursively.
- Safety: use -i or -I (prompt), -v (verbose). Avoid -f unless you know why.
- Directories: rm -r dir/; rm -d only for empty dirs; rmdir also removes empty dirs.
- Globs & quoting: quote names with spaces; use -- for dash-prefixed names.
- Find-based deletes: find … -delete or find … -print0 | xargs -0 rm.
- Symlinks: rm link deletes the link, not its target.
- Recovery: prefer trash-put or git rm to keep a way back while learning.
Want me to package rm + cp + mv into a 1-page printable for your class?