rm
rm
: remove files or directories.
The rm
command is used to delete files and directories from the filesystem. Because it directly removes files without sending them to a "trash" or "recycle bin," it's a powerful but potentially dangerous tool. Once a file is deleted using the rm
command, recovering it can be difficult or impossible, especially on systems without a dedicated backup solution.
Be very careful when using the rm
command, especially with the -r
(recursive) or -f
(force) options, as it permanently deletes files and directories without sending them to the recycle bin or trash. Double-check the file or directory names before running the command to avoid accidentally deleting important data.
If you're not sure, use the -i
(interactive) option when deleting multiple files or directories to prompt for confirmation before each removal. This can help prevent accidental deletions.
Remember that you can use the Tab key to autocomplete file and directory names in the terminal.
For example, you can type rm f
and then press Tab to autocomplete the command to rm file.txt
. This is a great way to avoid typos and speed up your work.
Examples
Linux/macOS
Delete a single file:
rm file.txt
Delete multiple files:
rm file1.txt file2.txt file3.txt
Delete a directory and its contents:
rm -r path/to/directory
Forcefully delete a directory without prompts or warnings. Bypass write protection. Don't show errors if the file/directory doesn't exist:
rm -rf path/to/directory
Delete files in interactive mode (ask for confirmation before each file deletion):
rm -i path/to/file.txt
Windows (PowerShell)
In Windows PowerShell, the equivalent command to rm
is Remove-Item
. The behavior and options are similar to Unix systems.
Remove a single file in Windows PowerShell:
rm file.txt
Remove multiple files in Windows PowerShell:
rm file1.txt, file2.txt
Remove an empty directory:
rm -Path path\to\empty_directory
Remove a non-empty directory and its contents recursively:
rm -Path path\to\directory -Recurse
Force removal without prompting for confirmation:
rm -Path path\to\file.txt -Force
Prompt for confirmation before removing each file:
rm -Path path\to\file.txt -Confirm