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
- Delete files
- Remove directories
- Clean up temporary files
- Batch file deletion
- 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
Safe recursive removal:
rm -ri directory/
Force removal:
rm -f file
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