Sagar.BlogArticle
All posts
All posts
Linux

rm — Delete Files and Directories (No Recycle Bin!)

rm permanently deletes files — there's no Recycle Bin. Learn safe deletion practices, when rm -rf is appropriate, and how to protect yourself from accidents.

January 14, 20255 min read
LinuxFilesCommands

rm deletes files and directories permanently. There is no Recycle Bin, no Trash folder, no Undo. Once it's gone, recovering it requires specialised disk recovery tools (and even then, recovery isn't guaranteed).

Understand this command deeply before you use it in anger.

rm is Permanent

Linux has no built-in undo for rm. Deleted files do not go to a Trash folder — they are immediately unlinked from the filesystem. Recovery requires forensic tools and is not guaranteed, especially on SSDs with TRIM enabled.

Options

rm Options

OptionLong FormDescription
-r, -R--recursiveRemove directories and their contents recursively
-f--forceIgnore non-existent files, never prompt
-i--interactivePrompt before every removal
-IPrompt once if removing more than 3 files
-d--dirRemove empty directories
-v--verboseShow each file as it's removed

Danger Levels

Danger Scale

CommandRiskWhat it does
rm file.txt🟢 LowRemove a single known file
rm *.log🟡 MediumRemove all matching — verify with ls *.log first
rm -r folder/🟡 MediumRemove directory tree
rm -rf folder/🔴 HighForce-remove entire tree, no prompts
sudo rm -rf /☠️ CATASTROPHICNEVER DO THIS — destroys the entire system

Examples

rm-examples.sh
# ─── Remove files ────────────────────────────────────────────
rm file.txt                          # Delete single file
rm file1.txt file2.txt               # Delete multiple files
rm *.log                             # Delete all .log files (CAREFUL!)
rm -v file.txt                       # Verbose: shows "removed 'file.txt'"

# ─── Remove directories ──────────────────────────────────────
rm -r folder/                        # Remove directory and all contents
rm -rf folder/                       # Force-remove (no prompts)
rm -d empty_folder/                  # Remove ONLY if empty (safer)
rmdir empty_folder/                  # Alternative for empty directories

# ─── Safe removal ────────────────────────────────────────────
rm -i file.txt                       # Prompt: "remove 'file.txt'? y"
rm -I *.log                          # Prompt once for 3+ files
rm -ri folder/                       # Interactive recursive removal

# ─── Verbose removal ─────────────────────────────────────────
rm -rv old_project/                  # See every file as it's removed

Safety Practices

Always preview what you're about to delete before running rm with wildcards or -rf:

# ALWAYS preview wildcards first:
ls *.tmp                             # See what matches
rm *.tmp                             # Then remove

# Use echo for dry-run
echo rm -rf old_logs/                # Shows what WOULD run (doesn't delete)

# Add rm -i to .bashrc for interactive by default
echo "alias rm='rm -i'" >> ~/.bashrc

# Use trash-cli (install) instead of rm for recoverable deletes
sudo apt install trash-cli
trash file.txt                       # Moves to trash instead of permanent delete
trash-restore                        # Recover it

The -rf Pattern

rm -rf is the Linux way to clean up build artifacts, empty directories, and leftover files in scripts. It's fine and commonly used in controlled situations: rm -rf dist/ .next/ node_modules/. The danger is only when the path is wrong or comes from an untrusted variable. Always double-check your path before running rm -rf.


Quick Check

Before running `rm *.txt` on a directory with important files, what should you always do first?

Exercise

Safe deletion practice:

  1. Create a test directory: mkdir -p test_delete/logs
  2. Create some files: touch test_delete/{a,b,c}.txt test_delete/logs/{jan,feb}.log
  3. Preview before deleting: ls test_delete/*.txt
  4. Delete only the .txt files: rm test_delete/*.txt
  5. Clean up the entire directory with one command — but use verbose so you see each deletion