In the Ubuntu system, deleting files or directories is a daily operation, but improper operations may lead to loss of important data. The rm command is the most commonly used deletion tool, and mastering its correct usage is crucial. This article will guide you through the “correct posture” of using the rm command in a simple and understandable way.
1. Basic Function of the rm Command¶
rm is an abbreviation for “remove” and its core function is to delete files or directories. Important notes:
- By default, files/directories deleted with rm will not go to the “recycle bin” but will be directly removed from the disk, making them difficult to recover once deleted.
- It cannot delete read-only files (unless permissions are modified with chmod) or files that are already open (unless the file handle is released).
2. Most Basic Usage¶
1. Deleting a Single File¶
Format: rm filename
Example: Delete the file named test.txt
rm test.txt
After execution, test.txt will be directly deleted. If the file does not exist, the terminal will display an error: “No such file or directory”.
2. Deleting a Directory (Note: Directories require parameters)¶
Using rm directory name directly will report an error (“Is a directory”), because rm can only delete files by default. To delete a directory (including all its contents), use the parameter -r (recursive deletion, i.e., delete the directory and all its subfiles/subdirectories):
Format: rm -r directory name
Example: Delete the directory named docs (assuming it contains subfiles)
rm -r docs
3. Common Options: Making Deletion Safer and Controllable¶
rm has many options, and different parameters can be added to achieve more flexible deletion needs. The following are the 3 most commonly used options:
1. -i (Interactive, Confirm Before Deletion)¶
Asks for user confirmation before deletion to avoid accidental deletion.
Format: rm -i filename/directory name
Example: Confirm before deleting data.csv
rm -i data.csv
After execution, the terminal will prompt: rm: remove regular file 'data.csv'?, enter y and press Enter to confirm, or n to cancel deletion.
2. -f (Force, Bypass Error Warnings)¶
Ignores error prompts and directly deletes files/directories (even if the file does not exist or lacks permissions). Use with caution!
Format: rm -f filename/directory name
Example: Force delete a non-existent temp.log (no error will be reported)
rm -f temp.log
3. -v (Verbose, Show Deletion Process)¶
Shows specific operations during deletion, which is convenient for confirming which files are deleted.
Format: rm -v filename/directory name
Example: Delete multiple files and show the process
rm -v file1.txt file2.txt
After execution, the output will be: Removed 'file1.txt'、Removed 'file2.txt'.
4. Practical Operation Examples¶
Example 1: Delete a Single File (With Confirmation)¶
# Confirm before deletion
rm -i report.pdf
After entering y, report.pdf is deleted; entering n keeps it.
Example 2: Delete a Directory (Including Sub-Contents)¶
# Delete the directory named "old_project" (recursively delete all contents)
rm -r old_project
Example 3: Force Delete Multiple Files¶
# Force delete non-existent files (no error) or overwrite existing files
rm -f backup.txt temp.doc
Example 4: Delete All txt Files in the Current Directory¶
# Use wildcard * to match all txt files, -i can be added or not (recommended to add -i for confirmation)
rm -i *.txt
5. Must-Know “Pitfalls” and Safety Tips¶
1. Do NOT use rm * or rm -rf *!¶
This will recursively delete all files and subdirectories in the current directory. If there are important files (e.g., photos, documents), they will be directly cleared!
- Wrong Example: Executing rm -rf * in the desktop directory will instantly delete all desktop files.
- Correct Approach: If batch deletion is needed, first list the files to confirm: ls *.txt, then delete after confirmation.
2. Do NOT delete system-critical files¶
Files in directories such as /etc (system configuration), /bin (system commands), and /usr (user programs) may cause system failures if deleted randomly.
3. Confirm Directory Structure Before Deletion¶
If the directory contains multiple layers of subdirectories, rm -r will recursively delete all contents. For example:
# Assume the directory structure: docs/1.txt, docs/sub/2.txt
rm -r docs # Will delete docs and all its contents
Always use ls -l docs to check the directory contents before deletion to avoid accidentally deleting important subdirectories.
4. Use rmdir to Delete Empty Directories (Only Empty Directories)¶
rmdir is a command specifically for deleting empty directories and is safer than rm -r (cannot delete non-empty directories):
rmdir empty_folder # Only deletes empty directories
If the directory is not empty, rmdir will report an error: “Directory not empty”.
6. How to Recover After Accidental Deletion¶
If you accidentally delete a file, try the following methods (success rate is not high; act quickly):
1. Graphical Interface Recycle Bin: The “Trash” folder in Ubuntu may retain recently deleted files (only for files deleted via the graphical interface; files deleted via the terminal are not in the recycle bin by default).
2. Recovery Tools: Use extundelete (for ext4 file systems) or testdisk and other tools. First install:
sudo apt install extundelete
Then follow the tool instructions (e.g., specify the device and partition: extundelete /dev/sda5 --restore-all).
3. Note: Do not continue writing data after deletion (e.g., creating new files), as this may overwrite the original data and lead to recovery failure.
Summary¶
The rm command is a core tool for deleting files/directories in Ubuntu, but its “irreversible” nature requires extreme caution:
- Confirm the target before deletion (filename, directory structure), and use ls or tree to check contents when necessary.
- Prioritize -i (confirmation mode) to avoid accidental deletion.
- Avoid dangerous operations (e.g., rm *, rm -rf *), especially in system directories and important data locations.
Mastering the correct deletion method ensures data security!