rm: remove files
rm notes.txt
lsIn the terminal
student@vulntech:~$ rm notes.txt student@vulntech:~$ ls Documents backup report.txt
Common mistake: There is no recycle bin in the terminal. A file deleted with
rmis gone for good. Check twice before pressing Enter.
rmdir: remove an empty directory
mkdir empty
rmdir empty
rmdir DocumentsIn the terminal
student@vulntech:~$ mkdir empty student@vulntech:~$ rmdir empty student@vulntech:~$ rmdir Documents rmdir: failed to remove 'Documents': Directory not empty
rmdir only removes empty directories, which makes it safe.
rm -r: remove a directory and everything in it
rm -r Documents
lsIn the terminal
student@vulntech:~$ rm -r Documents student@vulntech:~$ ls backup notes.txt report.txt
Safety options
| Option | What it does |
|---|---|
-i | Ask before deleting each file |
-v | Print each file as it's deleted |
-r | Delete directories and everything inside them |
-f | Force: never ask, and don't complain about missing files |
rm -v notes.txt report.txtIn the terminal
student@vulntech:~$ rm -v notes.txt report.txt removed 'notes.txt' removed 'report.txt'
A word about rm -rf
rm -rf something deletes something and everything inside it, without asking a single question. It is useful, and it is the most dangerous command most people type. A stray space or a wrong path can delete far more than you meant to.
Tip: Before a big
rm, runlswith exactly the same arguments first, to see what would be deleted. On this lab machine, nothing you delete matters: press Reset to get everything back.
Remember:
rm file,rmdir empty-dir,rm -r dir. No undo!