Chapter 13: Bash Remove (rm)
BashRemove (rm)
Β It is the main command we use to permanently delete files and folders in Linux / macOS / Unix-like systems.
Very important warning first (teacher voice ON):
There is no Recycle Bin / Trash by default with rm. What you delete with rm is gone forever (unless you have special recovery tools or backups or snapshots). Many experienced developers have cried at least once because of rm.
So let’s learn it slowly and carefully, step by step, like good students π
1. Basic syntax
|
0 1 2 3 4 5 6 |
rm [options] file_or_folder_name ... |
You can give it:
- one file
- many files
- folders (but only with special option)
2. Most common & safest way β delete one normal file
|
0 1 2 3 4 5 6 7 |
# Let's say you have a file called notes.txt rm notes.txt |
That’s it. The file disappears immediately. No question asked.
3. The safest useful option: -i β ask me before deleting!
Many people add this by default (you can even make it automatic later).
|
0 1 2 3 4 5 6 |
rm -i important_document.pdf |
Terminal will ask:
|
0 1 2 3 4 5 6 |
rm: remove regular file 'important_document.pdf'? |
You must type y (yes) + Enter to really delete it. Type anything else (n, enter, Ctrl+C) β file is safe.
Very good habit when you’re learning!
4. Delete many files at once
|
0 1 2 3 4 5 6 |
rm file1.txt file2.txt photo.jpg old_report.pdf |
Or use wildcards (very powerful):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Delete all .txt files in current folder rm *.txt # Delete all files that start with backup_ rm backup_* # Delete everything that ends with .log (very common!) rm *.log # Be careful with this one: rm * # β deletes EVERYTHING in current folder (files + not-hidden folders) |
5. How to delete folders? β You need -r or -R
By default rm refuses to delete folders.
|
0 1 2 3 4 5 6 7 |
rm my_folder # rm: cannot remove 'my_folder': Is a directory |
Correct way:
|
0 1 2 3 4 5 6 7 8 |
rm -r my_folder # or rm -R my_folder # same thing (big R or small r β both work) |
-r = recursive = delete folder + everything inside + subfolders + files inside subfolders…
6. The famous (and scary) combination β rm -rf
|
0 1 2 3 4 5 6 7 |
rm -rf folder_name rm -rf /some/path/project_old |
What do the letters mean?
- -r β recursive (delete folders + content)
- -f β force = don’t ask ANY questions + don’t show errors if file doesn’t exist
So rm -rf means:
“Delete this folder and everything inside it β silently, without asking me anything.”
Very useful when cleaning up Very dangerous when you mistype the path
Classic horror story examples students must know
|
0 1 2 3 4 5 6 7 8 9 10 11 |
# These commands can destroy your system if run with sudo / root!!! rm -rf / # β deletes entire system (modern rm blocks this by default) rm -rf /* # β same idea rm -rf ~ # β deletes your whole home folder rm -rf . # β deletes everything in current folder (including .git, .configβ¦) |
7. Useful & safer variations people use every day
| What you want to do | Command you usually type | Explanation |
|---|---|---|
| Delete file & ask confirmation | rm -i file.txt | safest for important files |
| Delete folder quietly | rm -rf build/ | very common in programming |
| Delete folder but ask once (smart middle) | rm -rI node_modules/ | -I = ask only once if many files |
| Delete but show what is being deleted | rm -rv venv/ | -v = verbose/show names |
| Delete empty folder (alternative) | rmdir empty_folder | safer than rm -r for empty dirs |
| Force delete even write-protected files | rm -f important.txt | ignores permission prompts |
8. Quick safety table (memorize this!)
| Command | Asks before delete? | Can delete folders? | Dangerous level |
|---|---|---|---|
| rm file.txt | No | No | β ββββ |
| rm -i file.txt | Yes | No | β ββββ |
| rm *.log | No | No | β β βββ |
| rm -r folder | No | Yes | β β β ββ |
| rm -rf folder | No | Yes | β β β β β |
| sudo rm -rf / | No | Yes | β β β β β |
9. Bonus pro tips from your teacher
- Always double-check before pressing Enter on rm -rf
- Use echo rm -rf something first β it shows what would be deleted
- Many people create alias: alias rm=’rm -i’ in ~/.bashrc
- Want real trash bin? Install trash-cli β then use trash-put instead of rm
- Modern rm (since ~2015) protects against rm -rf / unless you add –no-preserve-root
Now β your turn!
Try these safe practice commands in a test folder:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
mkdir practice cd practice touch file1.txt file2.txt mkdir photos touch photos/vacation.jpg ls -la # Try deleting safely rm -i file1.txt rm -i *.txt # Now folder rm -ri photos |
Understood? Any part you want me to explain again? Or want dangerous examples in a virtual machine? π
