Skip to content

rm Command Cheat Sheet

rm (remove) deletes files and directories. Deleted files are generally not recoverable.


Synopsis

rm [OPTION]... [FILE]...

Basic Usage

Remove a File

rm document.txt

Remove Multiple Files

rm file1.txt file2.png

Removing Directories

rm by itself cannot remove directories.

Recursive Removal (-r)

Deletes the directory and everything inside it.

rm -r my_folder/

Safety Options

Interactive (-i)

Prompts before every removal.

rm -i file.txt
# Output: rm: remove regular file 'file.txt'?

Interactive Once (-I)

Prompts once if deleting more than 3 files or recursively. Less annoying, still safe.

rm -I *.jpg

Force (-f)

Ignore nonexistent files and never prompt.

rm -f missing_file.txt
# (No error)

The "Big Red Button": rm -rf

Combine -r (recursive) and -f (force).

rm -rf /path/to/directory
Warning: This deletes everything without asking. Double-check the path!

Protection: --preserve-root

By default, rm usually prevents you from running rm -rf /.

rm -rf /
# Output: rm: it is dangerous to operate recursively on '/'
# rm: use --no-preserve-root to override this failsafe

Patterns and Globs

Delete by Extension

rm *.log

Delete Starting With

rm temp_*

Delete Almost Everything

Delete everything in current folder except hidden files.

rm *

Delete hidden files too.

rm -rf .[^.]* *

Hard-to-Delete Files

File Starts with Dash (-)

rm thinks -filename is a flag. Use -- to stop flag parsing.

rm -- -badfile.txt
# OR
rm ./-badfile.txt

File with Spaces

rm "my file.txt"

Notes

  • Undelete: Linux filesystems (ext4, xfs) do not have an "Undelete" feature. Once rm unlinks the inode, the data is marked as free space.
  • Secure Delete: shred or wipe overwrites data before unlinking to prevent forensic recovery.