How to Delete Files Safely with the Linux rm Command
The rm command deletes files and directories. It is permanent — there is no trash can — so the first rule is to check before you run it. rm file.txt deletes one file, and rm -r folder/ deletes a whole directory tree. This guide covers the flags that keep rm safe.
Delete a single file
The basic form takes a file name. There is no undo, so make sure you have the right file.
Delete a directory with -r
Directories need the recursive flag. rm -r removes the folder and everything inside it. Combined with -f it never asks and never fails on missing files: rm -rf.
Confirm before deleting with -i
Add -i and rm asks before every deletion. It slows things down, but it is the safest way to delete several files at once.
Delete several files at once
rm accepts many files and wildcards. rm *.bak deletes every .bak file in the folder. Pair wildcards with a dry run using ls first so you see exactly what matches.
The -rf warning
rm -rf / attempts to delete your entire system, and mistakes like rm -rf ~ /var happen with a stray space. Re-read commands with -rf before pressing Enter. For empty directories only, rmdir is the safe alternative. See rm for the full reference.
Keep learning
Related guides: How to Create and Remove Directories with mkdir and rmdir · How to Find Files and Directories with the Linux find Command · How to Copy Files and Directories with the Linux cp Command. Read all tutorials on the Learn Linux blog.