Eight rm command examples that you may not know all about
Deleting a file is similar to copying/moving a file. In Linux, there is a dedicated Commandrm
To complete all deletion-related operations. In this article, we will use some easy-to-understand examples to discuss the basic usage of this command.
However, before we start, it is worth noting that all examples in this article have been tested in Ubuntu 16.04 LTS.
Linux rm command Overview
In layman's terms, we can think thatrm
Command is used to delete files and directories. The syntax of this command is as follows:
Rm [Option]... [file/directory to be deleted]...
The command instructions are as follows:
GUN versionrm
Command Manual documentation.rm
Delete each specified file. By default, the directory is not deleted.
When more than three files are deleted, or the option is provided.-r
,-R
Or--recursive
(LCTT: Indicates recursively deleting files in the directory), if-I
(LCTT: capital I) or--interactive=once
If you enable interaction oncerm
Command will prompt the user whether to continue the entire delete operation, if the user response is not OKy
), The entire command is terminated immediately.
In addition, if the deleted file is not writable and the standard input is a terminal-f
Or--force
Or provide-i
(LCTT: lowercase I) or--interactive=always
Option,rm
Will prompt the user whether to delete this file, if the user response is not OK (LCTT Note: No Responsey
), The file is skipped.
The following Q & A examples help you better understand the usage of this command.
Q1. how can I use the rm command to delete files?
This is very simple and intuitive. You only need to pass in the file name (if the file is not in the current directory, you also need to add the file path)rm
Command.
(LCTT Description: multiple file names can be input using spaces .)
Rm file 1 file 2...
For example:
rm testfile.txt
How to remove files using rm command
Q2. how to use
rm
Command to delete the directory?
If you try to delete a directory, you need to provide-r
. Otherwiserm
An error is thrown to indicate that you are trying to delete a directory.
(LCTT:-r
Recursively delete all files and directories in the directory .)
Rm-r [directory name]
For example:
rm-r testdir
How to remove directories using rm command
Q3. how do I receive confirmation before the delete operation?
You can use-i
.
Rm-I [file/directory]
For example, if you want to delete a directory named "testdir", you must confirm the deletion operation. You can do this:
rm-r -i testdir
How to make rm prompt before every removal
Q4. how can I make rm ignore non-existing files or directories?
If you delete a non-existing file or directory,rm
The command throws an error, such:
Linux rm command example
However, you can use-f
The "force" option allows this operation to be executed forcibly, ignoring the error message.
Rm-f [file...]
How to force rm to ignore nonexistent files
Q5. how can I allow rm to confirm deletion only in some scenarios?
Option-I
To ensure that only one confirmation is prompted when more than three files are deleted or when the directory is deleted recursively.
For example, the following shows-I
Option: When two files are deleted, no prompt is displayed. If more than three files are deleted, a prompt is displayed.
How to make rm prompt only in some scenarios
Q6. how does rm work when deleting the root directory?
Delete the root directory (/
) Is the least desired operation for Linux users. This is why the defaultrm
The command does not support recursive deletion in the root directory. (LCTT annotation: earlyrm
The command does not prevent this behavior .)
How rm works when dealing with root directory
However, if you have to complete this operation, you need to use--no-preserve-root
. When this option is provided,rm
The root directory will not be specially processed (/
.
If you want to know in which scenarios Linux users will delete their root directories, click here.
Q7. how can I allow rm to delete only empty directories?
If you needrm
Only empty directories are deleted when the directory is deleted. You can use-d
.
Rm-d [Directory]
Below-d
Option usage -- only the empty directory is deleted.
How to make rm only remove empty directories
Q8. how can I have rm display details of the current delete operation?
If you want rm to display the details of the current operation, use-v
Option.
Rm-v [file/directory]
For example:
How to force rm to emit details of operation it is refreshing
Conclusion
Consideringrm
The function provided by the command can be said to be one of the most frequently used commands in Linux (like cp andmv
). In this article, we have covered almost all the major options it provides.rm
The command has a learning curve, so before you start using this command in your daily work, you will need to spend some time practicing its options.