Linux rm Command

Remove Files and Directories - 10 Practical Examples with Detailed Explanations

⚠️ WARNING: The rm command permanently deletes files! There is no undo or recycle bin. Always double-check before executing!
Example 1

Remove a Single File

$ rm file.txt
Deletes a single file from the current directory. The file is permanently removed from the filesystem. Always verify the filename before executing to avoid accidental deletion.
Warning: This action is permanent and cannot be undone!
Example 2

Remove Multiple Files

$ rm file1.txt file2.txt file3.txt
Deletes multiple files in a single command. List all files separated by spaces. This is more efficient than running rm multiple times for individual files.
Tip: Use wildcards for patterns: rm *.txt (removes all .txt files)
Example 3

Interactive Mode - Confirm Before Deletion

$ rm -i file.txt
rm: remove regular file 'file.txt'? y
The '-i' option prompts for confirmation before deleting each file. This is a safety feature that helps prevent accidental deletions. Type 'y' to confirm or 'n' to skip.
Best Practice: Use -i when deleting multiple files or in critical directories
Example 4

Verbose Mode - Show What's Being Deleted

$ rm -v file.txt
removed 'file.txt'
The '-v' (verbose) option displays each file as it's being removed. This provides confirmation that the operation completed and shows exactly what was deleted.
Useful For: Logging deletions and verifying batch operations
Example 5

Force Deletion - Skip Prompts

$ rm -f protected_file.txt
The '-f' (force) option removes files without prompting, even if they are write-protected. It also ignores non-existent files without error messages. Use with extreme caution!
Danger: This bypasses all safety prompts - double-check your command before executing!
Example 6

Remove Empty Directory

$ rm -d empty_directory
The '-d' option removes empty directories. This only works if the directory contains no files or subdirectories. For non-empty directories, use -r instead.
Alternative: Use 'rmdir' command specifically designed for removing empty directories
Example 7

Recursive Deletion - Remove Directory and Contents

$ rm -r directory_name
The '-r' (recursive) option deletes a directory and all its contents, including subdirectories and files. This is one of the most powerful and dangerous rm operations.
EXTREME CAUTION: Always verify the directory path before using -r. Run 'ls -R directory_name' first to see what will be deleted!
Example 8

Remove Files Using Wildcards

$ rm *.log
Deletes all files matching the pattern. The asterisk (*) is a wildcard that matches any characters. This example removes all files with .log extension in the current directory.
Examples: rm *.tmp (all .tmp files), rm test* (all files starting with 'test')
Example 9

Safe Recursive Deletion with Confirmation

$ rm -ri directory_name
rm: descend into directory 'directory_name'? y
rm: remove regular file 'directory_name/file.txt'? y
Combines recursive (-r) and interactive (-i) options for maximum safety. You'll be prompted to confirm deletion of the directory and each file within it. Best practice for deleting important directories.
Recommended: Always use -ri when recursively deleting directories containing important data
Example 10

The Most Dangerous Command (NEVER RUN THIS!)

$ rm -rf /
This command attempts to recursively force-delete everything on your system starting from the root directory. Modern systems have protections against this, but variations can still cause catastrophic damage. NEVER execute this command!
CRITICAL WARNING: This is shown as an example of what NOT to do. Always verify your paths, especially when using -rf together. Use 'pwd' to check your location first!