To delete a file safely in Terminal, use:
rm -i filename.txt
The -i flag asks for confirmation before deleting.
Basic Delete Commands
rm file.txt # Delete a file
rm -i file.txt # Delete with confirmation
rm file1.txt file2.txt # Delete multiple files
Delete Folders
To delete a folder and its contents:
rm -r foldername/ # Delete folder recursively
rm -ri foldername/ # With confirmation for each file
The -r means "recursive" - it deletes the folder and everything inside it.
The Safe Way: Always Use -i
Make -i your default by adding an alias:
echo 'alias rm="rm -i"' >> ~/.zshrc
source ~/.zshrc
Now rm always asks for confirmation. When you're sure you don't need it, use \rm to bypass the alias.
Preview Before You Delete
Before deleting with wildcards, preview what matches:
ls *.tmp # See what would be deleted
rm *.tmp # Then delete
This is especially important with rm -r:
ls -la old_project/ # Check contents first
rm -r old_project/ # Then delete
Move to Trash Instead
The safest "delete" is moving to Trash:
mv file.txt ~/.Trash/
You can recover it from Trash if needed.
For convenience, create an alias:
alias trash="mv -t ~/.Trash/"
Or use the trash command from Homebrew:
brew install trash
trash file.txt # Moves to Trash instead of deleting
What rm Actually Does
rm doesn't move files to Trash - it removes them immediately. The data isn't truly gone (it could theoretically be recovered with special tools), but for practical purposes, it's deleted.
There's no built-in undo for rm.
The Dangerous Command
rm -rf / # NEVER run this
This attempts to delete everything on your computer. Modern macOS has some protection against this, but variations can still cause damage.
Dangerous patterns to avoid:
| Command | Why it's dangerous |
|---|---|
rm -rf / |
Deletes entire system |
rm -rf * |
Deletes everything in current folder |
rm -rf ~ |
Deletes your entire home folder |
rm -rf . |
Deletes current folder and contents |
The -f Flag
-f means "force" - no confirmation, no errors for missing files:
rm -f file.txt # Delete without asking
rm -rf folder/ # Delete folder without asking
Use -f only when you're certain. It's useful in scripts, but dangerous interactively.
Safe Deletion Checklist
- pwd - Confirm you're in the right directory
- ls - Preview what you're about to delete
- Use -i - Get confirmation prompts
- Start small - Delete one file first, not everything at once
- Avoid -f - Unless you're absolutely sure
Recover Deleted Files
If you deleted something with rm:
- Time Machine: If you have backups, restore from there
- Git: If it was in a repo, you might recover it from Git history
- Cloud sync: Check iCloud, Dropbox, etc. for copies
- Data recovery software: As a last resort, tools like Disk Drill might help (but no guarantees)
Delete Files Matching a Pattern
rm *.log # All .log files
rm temp* # Files starting with "temp"
rm -r *_backup/ # Folders ending in "_backup"
Always ls the pattern first to preview what matches.
Delete Old Files
Delete files older than 30 days:
find . -type f -mtime +30 -delete
Preview first (remove -delete):
find . -type f -mtime +30
Interactive Mode for Bulk Deletes
When deleting many files, -i asks for each one. That's tedious. Instead:
rm -I *.tmp # Asks once if deleting more than 3 files
Note: -I (capital i) is different from -i. Check if your version of rm supports it.
Keep Learning
Understanding how to safely manage files is fundamental to using Terminal effectively. The free course covers this and more.
Check it out at Mac Terminal for Humans.