rm, rm -r & rmdir
Delete with care
Lesson 7 of 20 · 8 min · Module 2: Shape your files
The terminal has no recycle bin. A deleted file is gone, so deleting is a moment to slow down.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Delete files and folders
- Prefer the safest command that does the job
- Check a target before deleting it
The idea
rm file deletes a file. rmdir folder deletes a folder only if it is empty, which makes it a safe choice. rm -r folder deletes a folder and everything inside it. Always run ls on the target first.
ls logs/old
rm -r logs/old
01rmdir is the safe default for folders
rmdir only removes empty folders. If a folder still has files, it stops with "Directory not empty", which is exactly the warning you want.
rmdir logsrmdir: failed to remove 'logs': Directory not empty02rm -r is powerful
rm -r removes a folder and all of its contents in one step. Combined with -f (force) it does not ask or complain. That is why rm -rf deserves extra care.
03Look, then delete
Run ls on the exact path you plan to remove. If the listing surprises you, stop. On real systems many people also use rm -i, which asks before each file.
Your turn
data/photo.jpg. Delete the empty Downloads folder with rmdir. Then delete the logs/old folder and its contents. Leave everything else in place.What the checks look for
- Deletes data/photo.jpg
- Removes Downloads with rmdir
- Deletes logs/old but keeps the other logs
Take this with you
Deleting is permanent: check with ls, then use the narrowest command.
Go a little further
Try rm on a folder without -r and read the error message it gives.