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]
- To Remove Empty Directories
- To Remove Non-empty Directories
- Prompting File Name Before Removing a File
- Report Each File after Removing
- Removing File Names including Special Characters
- Removing Files Containing Spaces
- Removing Files Starting with Dash
- Conclusion
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 OptionDescription--helpDisplay information about the command and exit.--versionOutputs the version information of the rm command and exit.-f, --forceDelete files and ignore files and arguments that do not exist without Provoking prompt.-iPrompt before removing file-IPrompt 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, -recursiveRemoves directories and their content in a recursive manner.-d, --dirRemove empty directories without using -r/-R/-recursive. rm -dir is equivalent to rmdir.-v, --verboseExplains what is happening by printing it on the screen.--one-file-systemWhen 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-rootDo 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:
rm filename
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 filename
Output
rm: remove write-protected regular file 'filename'?
However, the
rm filename2 option allows users to delete write-protected files without confirmation.
rm -f filename
Important: The
rm filename2 option 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:
rm *.txt
Note: Use the
rm filename4 command 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
rm filename5 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
rm filename6 option with the rm command:
rm -r dirname
Prompting File Name Before Removing a File
The
rm filename7 option is used to ask for confirmation before removing a file. User presses the
rm filename8 key to confirm and the
rm filename9 key to stop.
rm -i filename
rm filename0
When removing multiple files or directories, the
rm filename7 option can come in handy as the prompt will ask before deleting each file. For example:
rm filename1
Output
rm filename2
To get a single prompt for recursively or removing more than three files, use the
rm: remove write-protected regular file 'filename'?1 option. The output will show the number of files being removed.
rm filename3
Output
rm filename4
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: remove write-protected regular file 'filename'?2 option:
rm filename5
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.
Example
rm filename6
Output
rm filename7
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 filename8
Escaping each space using a backslash:
rm filename9
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: remove write-protected regular file 'filename'?3 may misinterpret the filename as an option.
There are two ways that you can do that:
Double dashes before the file name
rm filename0
Refer to the File Name with path
rm filename1
Conclusion
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.