Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete multiple files in batch in Linux system

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to delete multiple files in batch in the Linux system", the content of the explanation in the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to delete multiple files in batch in the Linux system"!

Rm-rf * is not easy to use when we want to delete tens of thousands or hundreds of thousands or even millions of files in a linux system because it takes a long time to wait. In this case, we can use the linux system command rsync to handle it cleverly. Rsync actually uses the replacement principle, and it takes seconds to delete hundreds of thousands of files.

1. Install rsync. Some systems have this command installed by default.

Ubuntu system:

The code is as follows:

Sudo apt-get install rsync

Fedora system:

The code is as follows:

Sudo yum install rsync

The rest can be installed in source code and downloaded from the following website

Http://rsync.samba.org

2. Rsync provides some parameters related to deletion.

Rsync-- help | grep delete

-del an alias for-delete-during

-- delete delete extraneous files from destination dirs

-- delete-before receiver deletes before transfer, not during

-- delete-during receiver deletes during transfer (default)

-- delete-delay find deletions during, delete after

-- delete-after receiver deletes after transfer, not during

-- delete-excluded also delete excluded files from destination dirs

-- ignore-errors delete even if there are Imax O errors

-- max-delete=NUM don't delete more than NUM files

Where-- the delete-before receiver deletes before transmission

3. Example

Empty the directory or file, as follows:

1. Create an empty directory first

The code is as follows:

Mkdir / data/blank

2. Delete the target directory with rsync

The code is as follows:

Rsync-- delete-before-d-a-H-v-- progress-- stats / data/blank/ / var/edatacache/

Or

The code is as follows:

Rsync-- delete-before-d / data/blank/ / var/edatacache/

In this way, the / var/edatacache directory is quickly cleared.

Option description:

-the delete-before receiver deletes before transmission

-progress shows the transmission process during transmission

-an archiving mode, which means that files are transferred recursively and all file attributes are maintained

-H files that maintain hard connections

-v detailed output mode

-stats gives the transfer status of some files

-d transfer directories without recursing

Delete folder

This method is useful if a large number of small files are concentrated in several directories.

The code is as follows:

Rm-f / var/log/httpd/access.log

The / var/log/httpd/access.log file will be forcibly deleted

-r is recursive downwards, and no matter how many directories there are, delete them as well.

-f means to delete directly and forcibly without giving any prompt.

-I for interactive deletion.

Tip: be careful when using the rm command. Because once a file is deleted, it cannot be restored. To prevent this from happening, you can use the I option to confirm the files to be deleted one by one. If the user enters y, the file will be deleted. If you enter anything else, the file will not be deleted.

Be careful when using this rm-rf to delete files. Linux does not have a Recycle Bin.

Rm-r directory name:

Delete all files in subdirectories and subdirectories

The code is as follows:

[root@localhost test] # ls-l

Total 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Drwxr-xr-x 2 root root 4096 10-26 14:51 test1

Drwxr-xr-x 3 root root 4096 10-25 17:44 test2

Drwxrwxrwx 2 root root 4096 10-25 17:46 test3

Drwxr-xr-x 2 root root 4096 10-25 17:56 test4

Drwxr-xr-x 3 root root 4096 10-25 17:56 test5

The code is as follows:

[root@localhost test] # rm-r test1

Rm: do you want to enter the directory "test1"? Y

Rm: do you want to delete the generic file "test1/log3.log"? Y

Rm: do you want to delete directory "test1"? Y

The code is as follows:

[root@localhost test] # ls-l

Total 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Drwxr-xr-x 3 root root 4096 10-25 17:44 test2

Drwxrwxrwx 2 root root 4096 10-25 17:46 test3

Drwxr-xr-x 2 root root 4096 10-25 17:56 test4

Drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test] #

Rm-rf directory name:

Delete all files in the subdirectory and all the files in the subdirectory without confirming one by one

The code is as follows:

[root@localhost test] # rm-rf test2

[root@localhost test] # ls-l

Total 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf

Drwxrwxrwx 2 root root 4096 10-25 17:46 test3

Drwxr-xr-x 2 root root 4096 10-25 17:56 test4

Drwxr-xr-x 3 root root 4096 10-25 17:56 test5

[root@localhost test] #

Thank you for your reading, the above is the content of "how to delete multiple files in batch in the Linux system". After the study of this article, I believe you have a deeper understanding of how to delete multiple files in batch in the Linux system. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report