🗑️ Command: rm (Remove)
The rm command is used to delete files and directories. It is one of the most powerful and dangerous commands in Linux because, by default, there is no "Trash Can" or "Recycle Bin" in the terminal. Once you delete something with rm, it is usually gone forever.
1. The "Why"
you will inevitably create temporary files, old drafts, or code that just doesn't work. rm allows you to clean up your workspace and free up disk space. On Arch Linux, you might also use it to remove old configuration files or clear out build directories.
2. Basic Syntax
rm [OPTIONS] NAME
- Example:
rm notes.txtdeletes a file named "notes.txt" in your current folder.
3. Most Useful Flags
Because rm is permanent, its flags are mostly about safety or forcing the command to handle directories.
| Flag | Name | What it does |
|---|---|---|
-r |
Recursive | Required to delete a folder and everything inside it. |
-i |
Interactive | Asks for confirmation before every single deletion (Safest). |
-f |
Force | Ignores non-existent files and never prompts for confirmation. |
-v |
Verbose | Explains what is being done (shows the name of each deleted file). |
4. Practical Examples
A. Deleting a Single File
rm script.js
B. Deleting a Folder (The Recursive Flag)
If you try to use rm on a folder without -r, it will give you an error. To delete a directory and all its contents:
rm -r old_project/
C. The "Safety First" Method
If you are using wildcards (like *) and are afraid of deleting the wrong thing, use -i:
rm -i *.java
Bash will ask: "remove notes.java?" You must type y to confirm.
D. Deleting Multiple Specific Files
rm file1.txt photo.png document.pdf
5. Pro-Tips
- The Golden Rule: Always double-check your command before pressing Enter, especially if you are using
sudo rm -rf. A typo in the path can destroy your entire operating system. - Use
lsFirst: A common pro-tip is to runlswith the same pattern first. Ifls *.txtshows the files you want to delete, then you can safely runrm *.txt. - Arch Linux / Development Context: When working with projects, you might use
rm -rf build/to clear out old compiled files and start a fresh build. - Empty Directories: If you only want to delete a directory if it is empty, use the command
rmdirinstead ofrm -r.
6. Summary Reference
| Goal | Command |
|---|---|
| Delete a file | rm filename |
| Delete a folder and its contents | rm -r folder_name |
| Delete without asking (Force) | rm -f filename |
| Ask before deleting | rm -i filename |
| Show what is being deleted | rm -v filename |