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.
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:
cd ~/project
touch scratch.txt temp.txt
rm -v scratch.txt
lsremoved 'scratch.txt'
README.md
data
docs
logs
src
temp.txtYou 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:
ls temp*.txt
echo temp*.txttemp.txt
temp.txtIf 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:
mkdir empty-folder
rmdir -v empty-folder
rmdir docsrmdir: removing directory, 'empty-folder'
rmdir: failed to remove 'docs': Directory not emptyThe 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:
cp -r data data-copy
rm data-copyrm: cannot remove 'data-copy': Is a directoryrm -rv data-copy
lsremoved 'data-copy/fruit.txt'
removed 'data-copy/scores.csv'
removed directory 'data-copy'
README.md
data
docs
logs
src
temp.txtThe -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:
rm -i temp.txt
rm: remove regular file 'temp.txt'? ySafety habits
- Check where you are with
pwdbefore deleting with relative paths. - Preview with
lsorechousing the same pattern. - Use
-vso you can see what disappeared. - Avoid
rm -rfunless you are certain of the path, and never use it with a pattern you have not previewed. - Be very careful with variables.
rm -rf "$DIR/"with an empty$DIRbecomesrm -rf /. Scripts should check that variables are set. - Keep backups of anything you cannot recreate.
- 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.
Ready for the next small win?
Mark the lesson complete when you are ready.