The rm command is a very important command used to remove files and directories in Linux. The rm command deletes a file. It is a command that should be used with caution as using this command recklessly may remove important files and data. Deleting files or directories in Linux is not as simple as Windows because once you delete the file, it does not go to the Trash but is removed completely, and recovery is very hard or impossible.
In this article, we will discuss the rm command and the different options we can use when removing files and directories.
Table of Contents
- What is rm Command Used for?
- rm Command Syntax
- rm Command Options
- Removing One File
- Force Removing Files without Confirmation
- Removing Multiple Files
- Removing Directories (Folders)
- Prompting File Name Before Removing a File
- Report Each File after Removing
- Removing File Names including Special Characters
What is rm Command Used for?
The rm command is used to remove files in Linux. It can remove specific file entries, multiple files, and selected files from a directory.
To remove files using rm, the user does not need to confirm or have read/write permissions. Although, the user must have write permission for the directory containing the file (to be removed).
rm Command Syntax
By default, the rm command removes files without confirmation. Once you’ve deleted the files, the content of the files or directories cannot be recovered. For this reason, we advise extreme caution while using the command to avoid any mistakes.
The syntax of the rm command is:
rm [OPTION]... [FILENAME]...
rm Command Options
|Rm Command Option||Description|
|--help||Display information about the command and exit.|
|--version||Outputs the version information of the rm command and exit.|
|-f, --force||Delete files and ignore files and arguments that do not exist without Provoking prompt.|
|-i||Prompt before removing file|
|-I||Prompt before removing more than three files or removing files recursively. Less intrusive as compared to the -i option while providing protection against frequent mistakes.|
|-interactive [=WHEN]||Prompts according to the ‘when’ conditions: never, once (-I), and always (-i). Without a specific ‘when’ condition, prompt always.|
|-r, -R, -recursive||Removes directories and their content in a recursive manner.|
|-d, --dir||Remove empty directories without using -r/-R/-recursive. rm -dir is equivalent to rmdir.|
|-v, --verbose||Explains what is happening by printing it on the screen.|
|--one-file-system||When removing a hierarchy recursively, skips directories that are on a filesystem different from that of the corresponding command-line argument.|
|--preserve-root [=all]||Do not remove the root directory (/); coupled with ‘all’, rejects command-line argument on a separate device from its parent’s.|
|--no-preserve-root||Do not give any special treatment to the root directory.|
Removing One File
The basic rm command deletes one file. To do that, use the rm command paired with the name of the file you want to remove:
Here one of the two cases happen:
- An “Operation not permitted” error will appear on the screen if the you don’t have permissions on the parent directory
- The file will be removed without any notice if it is not write-protected.
Force Removing Files without Confirmation
If a file is write-protected, then the output will first prompt you for confirmation:
rm: remove write-protected regular file 'filename'?
-f option allows users to delete write-protected files without confirmation.
rm -f filename
-foption does not work with write-protected directories.
Removing Multiple Files
The rm command allows users to remove multiple files at once. For that, to pass each file name separated by a space as an argument to the rm command.
rm file1 file2 file3 file4
You can also use filename pattern matching with rm command in order to remove a specific type of file. To remove all text files in the current directory:
Note: Use the
lscommand to list the files before removing them. That way you will be able to see and move any files that you want to keep.
Removing Directories (Folders)
rm command can be used to remove directories. However, we use two different methods to remove an empty and non-empty file.
To Remove Empty Directories
To remove an empty directory using the rm command, use the
-d option (works similar to the rmdir command):
rm -d dirname
To Remove Non-empty Directories
The non-empty directories and their content is removed recursively using the
-r option with the rm command:
rm -r dirname
Prompting File Name Before Removing a File
-i option is used to ask for confirmation before removing a file. User presses the
y key to confirm and the
n key to stop.
rm -i filename
rm: remove regular file 'filename'? y
When removing multiple files or directories, the
-i option can come in handy as the prompt will ask before deleting each file. For example:
rm -i *
rm: remove regular file 'file1'? y rm: remove regular file 'file2'? y rm: remove regular file 'file3'? y
To get a single prompt for recursively or removing more than three files, use the
-I option. The output will show the number of files being removed.
rm -I file1 file2 file3 file4
rm: remove 4 arguments?
Report Each File after Removing
The rm command usually deletes the files silently. If you want to get a report of each file that is being removed, then use a
rm -v filename
Using the verbose option every time you remove a file is a good idea. That way you will know which file is deleted and not be caught by surprise.
rm -v file1 file2 file3 file4
removed 'file1' removed 'file2' removed 'file3' removed 'file4'
Removing File Names including Special Characters
Removing Files Containing Spaces
When removing files containing spaces you need to use the appropriate syntax.
Surrounding the filename in quotes:
rm -iv 'file name with spaces'
Escaping each space using a backslash:
rm -iv filename\ with\ spaces
Important: Creating file names including special characters or spaces is not considered good practice.
Removing Files Starting with Dash
Removing file names starting with a dash is a challenge because the
rm may misinterpret the filename as an option.
There are two ways that you can do that:
Double dashes before the file name
rm -- -filename
Refer to the File Name with path
rm -iv /home/-filename.png
The rm command is an excellent way to remove files, but Linux offers other commands such as unlink to remove files. To learn more about the rm command, visit the Linux manual pages.