rm — Remove Files & Directories Linux Destructive
rm (remove) deletes files and directories from your filesystem. It’s fast and powerful—and that’s exactly why it’s dangerous: in most Linux setups, deleted files do not go to a recycle bin.
Use rm for cleanup (logs, cache, backups) and for removing unwanted files (broken uploads, old builds, malware artifacts). But always operate with a “verify-first” mindset.
Once you run rm, recovery is not guaranteed. On VPS/server workloads, assume it’s permanent unless you have backups or snapshots.
Core Syntax
rm [OPTION]... FILE...
- Deletes one or more files.
- For directories, you must use recursion:
rm -r DIR
Options That Matter (Use Carefully)
| Option | Meaning | When to Use |
|---|---|---|
| -- | -- | |
-r | Remove directories recursively | Delete folders like wp-content/cache/ |
-f | Force: ignore missing files, never prompt | Automation or stubborn deletes (high risk) |
-i | Prompt before each removal | Manual safety for critical files |
-I | Prompt once if removing many files | Safer bulk operations |
-v | Verbose: print what’s removed | Logging/auditing in scripts |
--no-preserve-root | Allows deleting / | Never use in normal work |
--help | Show help | Quick reference |
--version | Show version | Compatibility checks |
rm -rf is commonly used—and commonly misused. One wrong path can wipe a project, a site, or a server.
Safe Workflow
A practical habit that prevents accidents:
- Preview what will be deleted (
ls,find ... -print) - Use protection flags when unsure (
-ior-I) - Delete the smallest scope possible (target a directory, not
/var/wwwbroadly) - Verify results (
ls, disk usage)
- Preview first
- Safer delete (interactive)
- Bulk delete (prompt once)
- Clear folder contents only
ls -lah *.log
rm -i wp-config.php
rm -I *
rm -rf wp-content/cache/*
Best Practices
- Always preview with
lsbefore using wildcards (*,?.log, etc.). - Use
-i(manual) or-I(bulk) when deleting anything important. - Prefer deleting contents (
dir/*) rather than deleting the folder itself if apps expect the folder to exist. - Avoid running destructive commands as root unless you absolutely must.
- For automated cleanup, log deletions with
-vand redirect output to a file.
Use pwd before a destructive command to confirm your current directory.
pwd
Troubleshooting
| Symptom | Likely Cause | Fix |
|---|---|---|
| - | - | |
Is a directory | You tried to remove a folder without recursion | Use rm -r DIR |
Permission denied | File owned by root or restricted permissions | Use sudo rm ... or fix ownership |
| Deleted wrong files | Wildcards matched more than expected | Preview with ls first; use -i/-I |
| Folder not empty | Needs recursive delete | Add -r |
Quick safety checks (HTML)
# Confirm location
pwd
# Preview what wildcard matches
ls -lah wp-content/cache/*
# Check ownership/permissions
ls -ld wp-content/cache
Cheat Sheet
rm file.txt # Delete single file
rm file1 file2 # Delete multiple files
rm -r folder # Delete directory recursively
rm -rf folder # Force delete without prompt (danger)
rm -i wp-config.php # Interactive delete (safer)
rm *.log # Delete all log files in current dir
rm -rf wp-content/cache/* # Delete contents, keep folder
Mini Quiz
- What does
rm -rdo that plainrmcannot? - Which option forces deletion without prompting?
- Why is
rm -rf /dangerous? - How would you delete only the contents of
wp-content/cache/but keep the folder?
- It removes directories recursively. 2)
-f. 3) It can wipe the entire filesystem. 4)rm -rf wp-content/cache/*.
Worked Examples (with expected output)
Don’t copy/paste destructive commands on production unless you are 100% sure of the path.
Remove a single file
rm error_log
Expected output: (silent success)
Remove multiple files
rm file1.txt file2.txt style.css
Expected output: (silent success)
Remove a directory without -r (failure)
rm wp-content/uploads
Expected output:
rm: cannot remove 'wp-content/uploads': Is a directory
Remove a directory recursively
rm -r wp-content/cache
Expected output: (silent success)
Force delete without prompt (high risk)
rm -rf wp-content/cache
Expected output: (silent success)
Before running, validate:
ls -ld wp-content/cache
Interactive deletion
rm -i wp-config.php
Expected output:
rm: remove regular file 'wp-config.php'?
Bulk confirmation with -I (prompt once)
rm -I *
Expected output (example):
rm: remove 150 arguments?
Verbose output
rm -v old-backup.zip
Expected output:
removed 'old-backup.zip'
Combine -rv
rm -rv logs/
Expected output (example):
removed directory 'logs/error'
removed directory 'logs'
Delete all .log files in current directory
rm *.log
Expected output: (silent success)
ls *.log
Delete .tmp files in uploads via wildcard
rm wp-content/uploads/*.tmp
Expected output: (silent success)
Delete hidden file
rm .htaccess
Expected output: (silent success)
Delete multiple extensions
rm *.bak *.old
Expected output: (silent success)
Delete files older than 7 days (using find)
find /var/www/html -name "*.log" -mtime +7 -exec rm {} \;
Expected output: (silent success)
find is safer than wildcardsYou can precisely target by name and age, rather than relying on what * matches.
Delete with sudo (root-owned file)
sudo rm /var/log/apache2/error.log
Expected output: (silent success)
Delete a specific month folder (keep parent)
rm -rf wp-content/uploads/2025/09
Expected output: (silent success)
Delete all contents but keep the folder
rm -rf wp-content/cache/*
Expected output: (silent success)
Delete .zip files via pipe (xargs)
ls *.zip | xargs rm
Expected output: (silent success)
xargs can behave badly with weird filenames. Prefer:
rm -- *.zip
Dangerous example (awareness only)
rm -rf /
Expected output (example safety guard):
rm: it is dangerous to operate recursively on '/'
Bypassing this protection with --no-preserve-root can destroy the whole system.
Safe delete logging (track what you removed)
rm -v *.log >> deletion.log
Expected output (example):
removed 'error.log'
removed 'debug.log'