Cleaning up is part of the job. Unix gives you rm and rmdir to delete things, but here's the warning you'll see in every Unix tutorial: there is no trash can. When you delete something in the terminal, it's gone. No undo. No recycling bin. The bits are free.
Removing files โ rm
rm deletes files. That's it.
$ rm notes.txt
$ ls
Gone. The file doesn't go to a trash folder โ it's wiped. Always double-check before hitting Enter.
Try it Yourself โRemoving directories โ rm -r
rmdir only works on empty directories. For directories with content, you need rm -r (recursive).
$ rm -r projects/
This deletes the directory and everything inside it โ all files, all subdirectories, everything. There's no confirmation by default. One moment it's there, the next it isn't.
The force flag โ rm -f
-f stands for "force." It tells rm to not ask questions and to ignore errors (like files that don't exist). Combined with -r, you get the nuclear option:
$ rm -rf temp/
rm -rf is probably the most dangerous command in Unix. It's also incredibly useful when you know what you're doing. Just be absolutely certain before you run it. I've seen people accidentally wipe their entire home directory with a misplaced rm -rf.
Safe habits
ls before you rm โ see what you're about to delete
- Use
rmdir for directories you know are empty
- Type the full path when deleting important things โ avoid wildcards if you're tired
- Consider aliasing
rm to rm -i (interactive) if you're nervous โ it'll ask before each delete
ls before you rm โ see what you're about to deletermdir for directories you know are emptyrm to rm -i (interactive) if you're nervous โ it'll ask before each delete