Introduction

Hello! Today I’ll explain the rm command.

The rm command removes (deletes) files and directories. Super useful, but there’s no undo - so be careful!

Used wisely, it’s fine. But never run something like rm -rf / - that’s a disaster waiting to happen!

What is the rm Command

The rm command is an external command for removing files and directories. “rm” stands for “remove”.

Deleted files don’t go to a trash bin - they’re immediately gone. Recovery is basically impossible, so use with caution.

Basic Syntax

1
rm [options] filename...

Main Options

Option Description
-i Prompt before removal (interactive)
-f Force removal, ignore errors (force)
-r, -R Remove directories recursively
-d Remove empty directories
-v Verbose output
--preserve-root Prevent removing root directory (default)
--no-preserve-root Allow removing root directory (dangerous!)

Usage Examples

Example 1: Remove a File

1
rm file.txt

Output:

1
(No output on success)

Removes file.txt. The most basic usage.

Example 2: Remove Multiple Files

1
rm file1.txt file2.txt file3.txt

Output:

1
(No output on success)

Removes multiple files at once.

Example 3: Remove with Confirmation (-i)

1
rm -i important.txt

Output:

1
rm: remove regular file 'important.txt'?

Prompts before deletion. Press y to delete, n to cancel. Super important option!

Example 4: Remove with Wildcards

1
rm *.txt

Output:

1
(No output on success)

Removes all .txt files. Use carefully!

Example 5: Remove Empty Directory

1
rm -d empty_folder

Output:

1
(No output on success)

Or:

1
rmdir empty_folder

Removes an empty directory.

Example 6: Remove Directory Recursively

1
rm -r folder

Output:

1
(No output on success)

Removes folder and all its contents. No going back - be careful!

Example 7: Force Remove (-f)

1
rm -f protected_file.txt

Output:

1
(No output on success)

Removes write-protected files without confirmation. Dangerous - use with care!

Example 8: Verbose Output (-v)

1
rm -v file1.txt file2.txt

Output:

1
2
removed 'file1.txt'
removed 'file2.txt'

Shows which files were removed.

Example 9: Remove Directory with Confirmation

1
rm -ri folder

Output:

1
2
3
4
rm: descend into directory 'folder'? y
rm: remove regular file 'folder/file1.txt'? y
rm: remove regular file 'folder/file2.txt'? y
rm: remove directory 'folder'? y

Prompts for each file in the directory. Safe!

Example 10: Remove Backup Files in Bulk

1
rm *~ *.bak

Output:

1
(No output on success)

Removes backup files (~ and .bak files).

Example 11: Remove Old Log Files

1
rm -v old_*.log

Output:

1
2
removed 'old_2024_01.log'
removed 'old_2024_02.log'

Removes log files matching the pattern.

Example 12: Force Remove Directory

1
rm -rf temp_folder

Output:

1
(No output on success)

Removes temp_folder and all contents without confirmation. One of the most dangerous commands. Be very careful!

Tips & Notes

 Deletion is Permanent

1
2
3
4
5
# Once you run this, it's gone
rm important_file.txt

# No trash bin!
# Recovery is nearly impossible!

Deleted files basically can’t be recovered. Always have backups!

Make -i a Habit

1
2
# Always confirm before deleting
alias rm='rm -i'

Add this to ~/.bashrc to always get confirmation. Essential for beginners!

Dangerous Commands

1
2
3
4
5
# NEVER do these!
rm -rf /           # Deletes entire system
rm -rf /*          # Deletes entire system
rm -rf ~/*         # Deletes entire home directory
rm -rf ./*         # Deletes current directory (dangerous depending on location)

DO NOT run these commands. They will destroy your system.

Watch Out for Spaces

1
2
3
# Dangerous!
rm -rf /tmp /important   # Removes /tmp and /important (OK)
rm -rf /tmp / important  # Removes /tmp and / (root) (DISASTER!)

Wrong spacing can destroy your system.

Verify Wildcards First

1
2
3
4
5
# First check with ls
ls *.txt

# Then remove if OK
rm *.txt

Always verify with ls before using wildcards with rm.

Alternative for Directory Removal

1
2
3
4
5
# Remove empty directory (safe)
rmdir empty_folder

# Remove with contents (intent is clearer than rm -r)
rm -r folder

Trash-like Usage

1
2
3
4
5
6
7
8
# Create trash directory
mkdir -p ~/.trash

# Move instead of delete
mv unwanted_file.txt ~/.trash/

# Empty periodically
rm -rf ~/.trash/*

Avoid Forced Recursive Removal

1
2
3
4
5
6
# Dangerous
rm -rf folder

# Safer
rm -r folder        # Stops on protected files
rm -ri folder       # With confirmation

The combination of -f and -r is especially dangerous.

Practical Usage

Clean Up Development Environment

1
2
3
4
5
# Remove build artifacts
rm -rf build/ dist/ *.pyc __pycache__/

# Remove node_modules
rm -rf node_modules/

Log File Management

1
2
3
4
5
# Remove old logs (7+ days)
find /var/log -name "*.log" -mtime +7 -exec rm {} \;

# Or
find /var/log -name "*.log" -mtime +7 -delete

Remove Temporary Files

1
2
3
4
5
# Remove temp files in bulk
rm -f /tmp/myapp_*

# Clear cache
rm -rf ~/.cache/myapp/

Safe Deletion Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
# safe_rm.sh - Script with confirmation

if [ $# -eq 0 ]; then
    echo "Usage: $0 <file to delete>" >&2
    exit 1
fi

for file in "$@"; do
    if [ -e "$file" ]; then
        echo "Target: $file"
        ls -lh "$file"
        read -p "Really delete? (yes/no) " answer
        if [ "$answer" = "yes" ]; then
            rm -v "$file"
        else
            echo "Skipped: $file"
        fi
    else
        echo "Not found: $file"
    fi
done

Project Cleanup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash
# Clean up project directory

echo "Removing build artifacts..."
rm -rf build/ dist/ *.egg-info/

echo "Removing cache files..."
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null
find . -type f -name "*.pyc" -delete

echo "Done!"

Delete with Backup

1
2
3
4
5
# Backup before deletion
backup_dir="/backup/$(date +%Y%m%d)"
mkdir -p "$backup_dir"
cp -a important_folder "$backup_dir/"
rm -rf important_folder

Summary

Key points about the rm command:

  • Permanently deletes files and directories
  • -i: Confirm before deletion (essential!)
  • -r: Recursively remove directories
  • -f: Force removal (dangerous!)
  • -v: Verbose output
  • Deletion is irreversible
  • rm -rf is especially dangerous
  • Common combinations: rm -i, rm -ri, rm -v

Important: rm has no undo. Make -i a habit and always confirm before deleting. Don’t forget backups!