rm

Overview

The rm (remove) command deletes files and directories. It’s a powerful command that can recursively remove directory trees and includes safety features to prevent accidental deletions.

Syntax

rm [options] file...

Common Options

Option Description
-r, -R Remove directories recursively
-f Force removal (no prompting)
-i Interactive (prompt before removal)
-I Prompt once before removing many files
-d Remove empty directories
-v Verbose mode
--preserve-root Do not remove ‘/’ (default)
--one-file-system Stay on one filesystem
--no-preserve-root Allow removing ‘/’

Key Use Cases

  1. Delete files
  2. Remove directories
  3. Clean up temporary files
  4. Batch file deletion
  5. System cleanup

Examples with Explanations

Example 1: Remove File

rm file

Delete a single file

Example 2: Remove Directory

rm -r directory

Remove directory and contents

Example 3: Safe Remove

rm -i file

Remove with confirmation prompt

Understanding Output

  • No output by default
  • With -v:
    • ‘removed file’ messages
  • Error messages for:
    • Permission denied
    • No such file
    • Directory not empty
    • Operation not permitted

Common Usage Patterns

  1. Safe recursive removal:

    rm -ri directory/
  2. Force removal:

    rm -f file
  3. Remove empty directories:

    rm -d empty_dir/

Performance Analysis

  • Directory entry updates
  • Inode management
  • Filesystem considerations
  • Large directory impact
  • Security implications

Additional Resources

Safety Warning

⚠️ Use rm with caution: - Always verify the files to be deleted - Use -i for interactive mode - Be extremely careful with -r and -f - Consider using trash instead - Never run rm -rf / or similar commands