Skip to content

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.

Lesson 6 of 166 minSkip to the terminal ↓

rm (remove) deletes files:

rm downloads/setup.tmp

There'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-folder

To delete a directory and everything inside it, use rm -r (recursive):

rm -r old-project

You'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

  1. Run ls with the same name or pattern first, to see exactly what will go.
  2. Prefer specific names to patterns, and patterns to whole directories.
  3. 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 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)

Lesson 6 of 16.

learner@sandbox: ~bash (practice)
Note: Practice shell for this lesson. Type a command and press Enter (help lists them all).