Deleting Files

This is the command that can end careers. I'm not joking.

rm is powerful, unforgiving, and has no undo. Let's learn it properly.

The Basic rm

Terminal
$rm file.txt

The file is gone. Not in trash. Gone.

No Trash Can

Linux rm doesn't move files to trash. It removes them from the filesystem immediately. Recovery requires special tools and isn't guaranteed.

Delete Multiple Files

Terminal
$rm file1.txt file2.txt file3.txt
$rm *.log
(removes all .log files)
$rm old_*
(removes all files starting with old_)

The -i Flag (Interactive)

Always prompts for confirmation:

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

Alias Suggestion

Some people add this to their .bashrc:

hljs bash
alias rm='rm -i'

Now rm always asks. But I'd recommend against this - it teaches bad habits. Better to be deliberate about when you want protection.

Deleting Directories

rm alone won't delete directories:

Terminal
$rm mydir/
rm: cannot remove 'mydir/': Is a directory

-r (Recursive)

Deletes directories and their contents:

Terminal
$rm -r mydir/

Everything inside mydir/ is deleted, then the directory itself.

rmdir (Empty Directories Only)

A safer option for empty directories:

Terminal
$rmdir empty_folder/
$rmdir folder_with_files/
rmdir: failed to remove 'folder_with_files/': Directory not empty

rmdir refuses to delete directories that have content. Safer than rm -r.

The Dangerous Commands

rm -rf

The nuclear option:

hljs bash
rm -rf directory/
  • -r = recursive (delete contents)
  • -f = force (no prompts, no errors for missing files)
Terminal
$rm -rf old_project/
(instantly deleted, no questions asked)

The Most Dangerous Command

You've probably heard of rm -rf /. Don't even type it as a joke. Modern systems have protections, but the risk isn't worth it.

Horror stories:

  • rm -rf /usr /lib/nvidia (space instead of /) - destroys system
  • rm -rf $undefined_var/ - becomes rm -rf / if variable is empty
  • rm -rf ./* while in / - oops

Always double-check your path before running rm -rf.

Safe Deletion Practices

1. Use ls First

Terminal
$ls *.log
access.log error.log debug.log
$# Looks right? Now:
$rm *.log

Preview what would be deleted before actually deleting.

2. Check Your Path

Terminal
$pwd
/home/user/temp_files
$# Confirmed - safe to delete here
$rm -rf *

3. Use Variables Safely

hljs bash
# BAD - if $DIR is empty, this becomes rm -rf /
rm -rf $DIR/

# BETTER - fails if DIR is empty
rm -rf ${DIR:?DIR is not set}/

# BEST - double check first
if [ -d "$DIR" ]; then
  rm -rf "$DIR/"
fi

4. Move to Trash Instead

Some people create a safer alternative:

hljs bash
alias trash='mv -t ~/.trash/'

Then trash file.txt moves it to ~/.trash/ instead of deleting.

Quick Reference

CommandEffect
rm fileDelete a file
rm -i fileDelete with confirmation
rm -r dir/Delete directory and contents
rm -rf dir/Force delete, no prompts
rmdir dir/Delete empty directory only
Knowledge Check

What's the safest way to delete files matching a pattern?

Key Takeaways

  • rm is permanent - there's no trash can
  • Use rm -r for directories
  • rm -rf is dangerous - always double-check the path
  • Preview with ls before deleting with rm
  • Check pwd before running rm -rf *
  • Consider aliasing rm to rm -i if you're nervous (but learn proper habits)

Next: reading files without opening an editor.