Eight rm command examples that you may not know all about

Source: Internet
Author: User

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 CommandrmTo 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 thatrmCommand is used to delete files and directories. The syntax of this command is as follows:

  1. Rm [Option]... [file/directory to be deleted]...

The command instructions are as follows:

GUN versionrmCommand Manual documentation.rmDelete each specified file. By default, the directory is not deleted.

When more than three files are deleted, or the option is provided.-r,-ROr--recursive(LCTT: Indicates recursively deleting files in the directory), if-I(LCTT: capital I) or--interactive=onceIf you enable interaction oncermCommand 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-fOr--forceOr provide-i(LCTT: lowercase I) or--interactive=alwaysOption,rmWill 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)rmCommand.

(LCTT Description: multiple file names can be input using spaces .)

  1. Rm file 1 file 2...

For example:

  1. rm testfile.txt

How to remove files using rm command

 

Q2. how to use rmCommand to delete the directory?

If you try to delete a directory, you need to provide-r. OtherwisermAn error is thrown to indicate that you are trying to delete a directory.

(LCTT:-rRecursively delete all files and directories in the directory .)

  1. Rm-r [directory name]

For example:

  1. rm-r testdir

How to remove directories using rm command

 

Q3. how do I receive confirmation before the delete operation?

You can use-i.

  1. 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:

  1. 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,rmThe command throws an error, such:

Linux rm command example

However, you can use-fThe "force" option allows this operation to be executed forcibly, ignoring the error message.

  1. 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-ITo 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-IOption: 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 defaultrmThe command does not support recursive deletion in the root directory. (LCTT annotation: earlyrmThe 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,rmThe 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 needrmOnly empty directories are deleted when the directory is deleted. You can use-d.

  1. Rm-d [Directory]

Below-dOption 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-vOption.

  1. Rm-v [file/directory]

For example:

How to force rm to emit details of operation it is refreshing

 

Conclusion

ConsideringrmThe 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.rmThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.