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.
When you're inside a Git project, deleting a tracked file with rm appears as a removal in your working tree; you can confirm what changed with git status.
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.txtDelete multiple files:
rm file1.txt file2.txt file3.txtDelete a directory and its contents:
rm -r path/to/directoryForcefully 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/directoryDelete files in interactive mode (ask for confirmation before each file deletion):
rm -i path/to/file.txtWindows (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.txtRemove multiple files in Windows PowerShell:
rm file1.txt file2.txtRemove an empty directory:
rm -Path path\to\empty_directoryRemove a non-empty directory and its contents recursively:
rm -Path path\to\directory -RecurseForce removal without prompting for confirmation:
rm -Path path\to\file.txt -ForcePrompt for confirmation before removing each file:
rm -Path path\to\file.txt -Confirm