rm – remove files and directories

rm command remove files
rm command remove files

rm removes (deletes) files and directories. This is permanent — there’s no trash or undo.

Synopsis

rm [OPTIONS] FILE...

Common Options

OptionDescription
-r, -RRecursive (remove directories and contents)
-fForce, ignore nonexistent files, never prompt
-iPrompt before every removal
-IPrompt once before removing many files
-vVerbose, show files being removed
-dRemove empty directories

Examples

Remove a file

$ rm file.txt

Remove multiple files

$ rm file1.txt file2.txt file3.txt
$ rm *.log

Remove with confirmation

$ rm -i important.txt
rm: remove regular file 'important.txt'? y

Remove directory and contents

$ rm -r old_project/

Force remove (no prompts)

$ rm -rf temp_folder/

Verbose output

$ rm -rv build/
removed 'build/cache/file1.tmp'
removed 'build/cache/file2.tmp'
removed directory 'build/cache'
removed 'build/output.log'
removed directory 'build'

Remove files starting with dash

$ rm -- -badfilename
# Or
$ rm ./-badfilename

⚠️ Dangerous Commands

Be extremely careful with these:

# DANGEROUS - deletes everything in current dir
$ rm -rf *

# CATASTROPHIC - deletes entire system (don't run!)
$ rm -rf /

Most modern systems protect against rm -rf / but always double-check your path before running rm -rf.

Safer Alternatives

Use trash instead of rm

# macOS
$ brew install trash
$ trash file.txt  # Goes to Trash, recoverable

# Linux
$ sudo apt install trash-cli
$ trash-put file.txt

Interactive alias

# Add to ~/.bashrc
alias rm='rm -i'

Safe-rm package

# Prevents accidental deletion of critical paths
$ sudo apt install safe-rm

Tips

  • No undo: rm is permanent — consider using trash command
  • Test with ls first: ls *.log before rm *.log
  • Use -i for safety: Especially when learning
  • Avoid rm -rf wildcards: rm -rf ./temp/* not rm -rf / temp/* (space = disaster)
  • Use find for selective deletion: find . -name "*.tmp" -delete

See Also

  • rmdir — Remove empty directories only
  • mv — Move files (safer than delete)
  • find — Find and delete files selectively

Tutorials