rm – Remove Files and Directories
The rm command is used to delete files and directories. Use it with caution, because removed files are generally not recoverable.
Syntax
rm [OPTION]... FILE...
- You specify one or more files or directories to delete.
Common Options
| Option | Description |
|---|---|
-i |
Interactive: prompt before every removal |
-f |
Force removal without prompt and ignore nonexistent files |
-r or -R |
Recursive: remove directories and their contents |
-v |
Verbose: show files as they are removed |
--one-file-system |
When removing recursively, skip directories on other file systems |
Practical Examples
1. Remove a single file
rm file.txt
2. Remove multiple files at once
rm file1.txt file2.txt file3.txt
3. Prompt before deleting each file
rm -i file.txt
4. Remove an empty directory (use rmdir instead)
rmdir directory_name
5. Remove a directory and all its contents recursively
rm -r directory_name
6. Force remove a directory and its contents without prompts
rm -rf directory_name
7. Verbose removal showing all files deleted
rm -v file.txt
rm -rv directory_name
WARNING: Use with Extreme Caution
-
Never run
rm -rf *orsudo rm -rf *unless you are absolutely sure what you are doing. -
These commands recursively delete everything in the current directory and below, without any confirmation, and can easily wipe out your entire system or important data.
-
Running
sudo rm -rf *as root is especially dangerous because it has permission to delete critical system files and can make your system unusable. -
If you want safer deletions, always use the
-ioption to confirm each file removal:
rm -ri directory_name
- Alternatively, double-check your working directory before running destructive commands:
pwd
ls
Tips
-
Use
trash-clior similar tools if you want a “recycle bin” like safe delete. -
Be mindful of shell expansions: wildcards like
*or?can match many files unexpectedly. -
For safer scripting, avoid using
rm -rfwithout checks.
See Also
rmdir– remove empty directoriesfindwith-deleteor-exec rmfor advanced removal- Backup important files before deleting!
Summary
rm is a powerful and dangerous command to delete files and directories. Always double-check your command and consider using interactive mode (-i) or backups to prevent accidental data loss.