Cheat Sheet #day48 - rm

rm Command Cheatsheet
The rm command in Unix/Linux is used to remove files and directories. It stands for "remove" and can be a powerful tool when used with caution. Below are the basic and advanced usages of the rm command along with practical examples.
Basic Usage
Remove a file
rm filenameRemove multiple files
rm file1 file2 file3
Common Options
Interactive mode (prompt before every removal)
rm -i filenameForce removal (ignore nonexistent files and never prompt)
rm -f filenameRecursive removal (remove directories and their contents)
rm -r directorynameVerbose mode (explain what is being done)
rm -v filenameRemove directories (same as -r)
rm -d directoryname
Examples
Remove a single file
rm file.txtRemove multiple files
rm file1.txt file2.txt file3.txtRemove a directory and its contents
rm -r directorynameInteractive removal
rm -i file.txtForce removal of a file
rm -f file.txtVerbose removal
rm -v file.txt
Advanced Usage
Remove all files with a specific extension
rm *.txtRemove files using a wildcard
rm file*Remove empty directories
rm -d directorynameForcefully remove a directory and its contents
rm -rf directoryname
Practical Tips
Safety First: Use the
-ioption to prevent accidental deletions.Force Removal: Use the
-foption to force the removal of files without prompts, but use it cautiously.Recursive Removal: Always use
-ror-rfwith caution, especially when running as a root user, to avoid deleting critical system files.Verbose Mode: Use
-vto see exactly whatrmis doing, useful for debugging scripts.
Quick Reference
Remove a file:
rm filenameRemove multiple files:
rm file1 file2 file3Interactive mode:
rm -i filenameForce removal:
rm -f filenameRecursive removal:
rm -r directorynameVerbose mode:
rm -v filenameRemove directories:
rm -d directorynameForcefully remove a directory and its contents:
rm -rf directoryname
This cheatsheet covers the essential commands and options for using rm effectively, from basic file deletions to more advanced directory management tasks. Adjust the commands according to your specific requirements and environment.




