Delete files and folders safely: rm and rmdir
Practise deleting files with rm, empty folders with rmdir and whole folders with rm -r, in a sandbox where a mistake costs nothing. Learn the safe habits.
rm (remove) deletes files:
rm downloads/setup.tmpThere's no bin on the command line. Once rm has removed a file, it's gone. That's the whole reason for the habits below.
Directories
rmdir removes a directory, but only an empty one, which makes it the safe choice for tidying up:
rmdir empty-folderTo delete a directory and everything inside it, use rm -r (recursive):
rm -r old-projectYou'll often see rm -rf: -f (force) skips the errors and questions. Combined with a wrong name or a stray space it can delete far more than you meant, so leave -f off until you have a reason for it.
Safe habits
- Run
lswith the same name or pattern first, to see exactly what will go. - Prefer specific names to patterns, and patterns to whole directories.
- Keep a copy (
cp) of anything you can't get back.
In this practice terminal, Reset brings everything back. On a real computer, nothing does.
Your turn
Clean up: delete the two .tmp files in downloads (keep the PDFs), remove empty-folder, and remove old-project with everything in it.
Your task
Clean up: delete the two .tmp files in downloads (keep the PDFs), remove empty-folder, and remove old-project with everything in it.
- Delete both .tmp files, keep the PDFs (not done yet)
- Remove empty-folder (not done yet)
- Remove old-project and its contents (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
rm- Remove files or directories More on rm →
rmdir- Remove empty directories
rm -r- Remove directories and everything in them More on rm →