Deleting with rm and rmdir

rm: remove files

rm notes.txt
ls

In 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 rm is gone for good. Check twice before pressing Enter.

rmdir: remove an empty directory

mkdir empty
rmdir empty
rmdir Documents

In 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
ls

In the terminal

student@vulntech:~$ rm -r Documents
student@vulntech:~$ ls
backup  notes.txt  report.txt

Safety options

OptionWhat it does
-iAsk before deleting each file
-vPrint each file as it's deleted
-rDelete directories and everything inside them
-fForce: never ask, and don't complain about missing files
rm -v notes.txt report.txt

In 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, run ls with 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!

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…