Skip to lesson

Linux Command Line · Lesson 11 of 43

Deleting files and folders safely with rm

Delete files, empty folders and whole trees with rm and rmdir, preview with echo and ls first, and learn why the command line has no recycle bin.

Beginner to intermediate·11 min·Working with files and folders·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will delete files and folders confidently, and you will know the habits that stop a typing slip from becoming a disaster. Deleting is the one everyday command that cannot be undone, so it is worth learning carefully.

rm: remove files

rm removes files. There is no recycle bin on the command line. A removed file is gone, and recovering it requires special tools and luck. Use -v to see what was removed:

bash
cd ~/project
touch scratch.txt temp.txt
rm -v scratch.txt
ls
result
removed 'scratch.txt'
README.md
data
docs
logs
src
temp.txt

You can name several files, and the removal only affects the names you give. If a file does not exist, rm reports an error, and the -f option (force) suppresses that error, which is useful in scripts but dangerous at the keyboard.

Preview before you delete

The shell turns patterns such as *.txt into a list of names before rm ever sees them. That means you can check the list first by running a harmless command with the same pattern:

bash
ls temp*.txt
echo temp*.txt
result
temp.txt
temp.txt

If the list is what you expected, put rm in front of the same pattern. This one habit prevents most accidents. Press the Up arrow to recall the command, replace ls with rm, and run it.

rmdir: empty folders only

rmdir removes a folder only if it is empty, so it cannot destroy data by accident:

bash
mkdir empty-folder
rmdir -v empty-folder
rmdir docs
result
rmdir: removing directory, 'empty-folder'
rmdir: failed to remove 'docs': Directory not empty

The second command failed because docs contains files. That refusal is the safety feature.

rm -r: remove a folder and its contents

To delete a folder with everything inside, add -r (recursive). Without it, rm refuses to touch a directory:

bash
cp -r data data-copy
rm data-copy
result
rm: cannot remove 'data-copy': Is a directory
bash
rm -rv data-copy
ls
result
removed 'data-copy/fruit.txt'
removed 'data-copy/scores.csv'
removed directory 'data-copy'
README.md
data
docs
logs
src
temp.txt

The -r option goes through every subfolder, and the whole tree vanishes with no further question. The combination rm -rf (recursive and force) is the one you read about in horror stories, because it never asks and ignores errors.

Asking for confirmation

-i makes rm ask about every file. It is slow for large deletions but good while learning, or when working in an important folder. A middle road is -I (capital i), which asks once before deleting more than three files or when deleting recursively. In an interactive terminal you answer y or n:

text
rm -i temp.txt
rm: remove regular file 'temp.txt'? y

Safety habits

  1. Check where you are with pwd before deleting with relative paths.
  2. Preview with ls or echo using the same pattern.
  3. Use -v so you can see what disappeared.
  4. Avoid rm -rf unless you are certain of the path, and never use it with a pattern you have not previewed.
  5. Be very careful with variables. rm -rf "$DIR/" with an empty $DIR becomes rm -rf /. Scripts should check that variables are set.
  6. Keep backups of anything you cannot recreate.
  7. Think twice before using sudo rm. Administrator rights remove the protections that normally stop you from deleting system files.

Modern GNU rm refuses to remove / itself unless you add --no-preserve-root, but it will happily delete everything below your home folder.

Alternatives to deleting

If you are unsure, move instead of delete: mv old-folder ~/trash-for-now, and delete it later. Some systems have a trash-cli package that provides a recycle bin from the command line. For important data, snapshots and backups are the real safety net.

Common mistakes

A space inside a pattern. rm * .txt is two arguments: everything (*) and a file named .txt. This is how people delete whole folders. Check for stray spaces.

Running rm -rf in the wrong folder. Relative paths depend on where you are.

Trusting the shell prompt. Two terminal windows can look identical but be in different folders.

Deleting with sudo to silence an error. A "permission denied" message is usually telling you something important.

Exercise

In a practice folder, create five files named log1.txt to log5.txt with touch log{1..5}.txt. Preview log[1-3].txt with ls, then remove it with rm -v. Make a folder junk containing a file, try rmdir junk, then remove it correctly with rm -r. Finally, write one sentence about the habit you will use before every delete.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 Where do files deleted with rm go?
02 What does rmdir do to a folder that contains files?
03 What is a safe way to check what rm *.log would delete?
04 What does the -i option of rm do?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses