Deleting Files
This is the command that can end careers. I'm not joking.
rm is powerful, unforgiving, and has no undo. Let's learn it properly.
The Basic rm
The file is gone. Not in trash. Gone.
No Trash Can
Linux rm doesn't move files to trash. It removes them from the filesystem immediately. Recovery requires special tools and isn't guaranteed.
Delete Multiple Files
The -i Flag (Interactive)
Always prompts for confirmation:
Alias Suggestion
Some people add this to their .bashrc:
alias rm='rm -i'
Now rm always asks. But I'd recommend against this - it teaches bad habits. Better to be deliberate about when you want protection.
Deleting Directories
rm alone won't delete directories:
-r (Recursive)
Deletes directories and their contents:
Everything inside mydir/ is deleted, then the directory itself.
rmdir (Empty Directories Only)
A safer option for empty directories:
rmdir refuses to delete directories that have content. Safer than rm -r.
The Dangerous Commands
rm -rf
The nuclear option:
rm -rf directory/
-r= recursive (delete contents)-f= force (no prompts, no errors for missing files)
The Most Dangerous Command
You've probably heard of rm -rf /. Don't even type it as a joke. Modern systems have protections, but the risk isn't worth it.
Horror stories:
rm -rf /usr /lib/nvidia(space instead of/) - destroys systemrm -rf $undefined_var/- becomesrm -rf /if variable is emptyrm -rf ./*while in/- oops
Always double-check your path before running rm -rf.
Safe Deletion Practices
1. Use ls First
Preview what would be deleted before actually deleting.
2. Check Your Path
3. Use Variables Safely
# BAD - if $DIR is empty, this becomes rm -rf /
rm -rf $DIR/
# BETTER - fails if DIR is empty
rm -rf ${DIR:?DIR is not set}/
# BEST - double check first
if [ -d "$DIR" ]; then
rm -rf "$DIR/"
fi
4. Move to Trash Instead
Some people create a safer alternative:
alias trash='mv -t ~/.trash/'
Then trash file.txt moves it to ~/.trash/ instead of deleting.
Quick Reference
| Command | Effect |
|---|---|
rm file | Delete a file |
rm -i file | Delete with confirmation |
rm -r dir/ | Delete directory and contents |
rm -rf dir/ | Force delete, no prompts |
rmdir dir/ | Delete empty directory only |
What's the safest way to delete files matching a pattern?
Key Takeaways
rmis permanent - there's no trash can- Use
rm -rfor directories rm -rfis dangerous - always double-check the path- Preview with
lsbefore deleting withrm - Check
pwdbefore runningrm -rf * - Consider aliasing
rmtorm -iif you're nervous (but learn proper habits)
Next: reading files without opening an editor.