RM Command Delete directory and non-empty directory in Linux

Source: Internet
Author: User

RM command

Creating files in Linux is easy, and files can become obsolete and useless in the system at any time. Users can delete them with the RM command. The function of this command is to delete one or more files or directories in a directory, and it can also delete a directory and all the files and subdirectories under it. For linked files, only the link is deleted and the original file remains unchanged.
The general form of the RM command is:
RM [Options] File ...
If the-r option is not used, RM does not delete the directory.
The options for this command have the following meanings:
-F ignores files that do not exist and never gives a hint.
-R instructs Rm to recursively delete all directories and subdirectories listed in the parameters.
-I for interactive deletion.
Use the RM command with extra care. Because once a file is deleted, it cannot be restored. For example, when a user enters a cp,mv or other command, it accidentally enters the RM command, and when the user presses the ENTER key and realizes his or her own error, it is too late and the file is gone. To prevent this, you can use the I option in the RM command to confirm each file that you want to delete. If the user enters Y, the file is deleted. If you enter anything else, the file will be retained. In the next example, the user deletes the file test and example. You will then be asked to confirm each file. The user eventually decides to delete the example file and keep the test file.

The code is as follows Copy Code
$ RM–II Test Example

There are more files in a directory, consider deleting them all at once.

The code is as follows Copy Code

[Root@hx relate]# RM * F
Rm:invalid option--?
Try ' RM./-Kunming China _relate.txt ' to remove the file ' -300245303367326320271372_relate.txt '.
Try ' rm--help ' for the more information.

This file is not deleted?

RM--help looked at the help

The code is as follows Copy Code

By default, RM does not remove directories. Use the--recursive (-R Or-r)
Option to remove each listed directory, too, along and all of its contents.

To remove a file whose name starts with a '-', for example '-foo ',
Use one of these commands:
RM---Foo

Rm./-foo


According to the tip:

The code is as follows Copy Code

[Root@hx relate]# rm./-Kunming China _relate.txt
Rm:remove regular file './-300245303367326320271372_relate.txt '? Y

Delete this file.

With: Bulk Delete a directory of empty files (0-byte file)

The code is as follows Copy Code
Find. -name "*"-type f-size 0c | Xargs-n 1 rm-f

Modify the corresponding-size parameter to delete a file of the specified size, such as deleting a file of 1k size:

The code is as follows Copy Code

Find. -name "*"-type f-size 1024c | Xargs-n 1 rm-f

But be careful not to use the-size 1k, this gets is occupies the space 1k, is not the file size 1k

If you delete the folder or the name of the connection, you can change the corresponding-type parameters, see the details of the man found.

The difference between RM and rmdir is that RM can delete an empty directory, and a non-empty directory cannot be deleted by RmDir, rm-rf a non-empty directory name.

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.