English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Sharing Methods for Correctly and Quickly Deleting Large Numbers of Files in Linux

Preface

The 'massive' mentioned in this article does not refer to the size, but to the quantity, for example, there are several million small files in a directory.

Recently, while optimizing the server, I found that there are a large number of files in the maildrop directory and clientmqueue directory under postfix. It is foolish to enter these directories and use the 'ls' command, and it is directly executing 'rm' * There is no reaction, and the number of files has not decreased, which means that directly using the 'rm' command to delete in a large number of file directories is invalid.

Then what is the correct method? There are two options available:

First method:

find /path/to/directory -type f -exec rm {} \;

Second method:

ls -1 /path/to/directory | xargs -I{} rm {}

The above two methods can successfully delete a large number of files, and the speed is also very fast. But there is a better method, for example, to delete the clientmqueue directory mentioned above, which is all emails, use the following method:

service sendmail stop
cd /var/spool
mv clientmqueue clientmqueue-todelete
mkdir clientmqueue
chown --reference=clientmqueue-todelete clientmqueue
chmod --reference=clientmqueue-todelete clientmqueue
service sendmail start
rm -rf clientmqueue-todelete

The above method renames the directory and then uses --Use the 'reference' parameter to rebuild the directory, and then delete the renamed directory. The method of directly deleting the directory is very fast. You can also keep the backup without deleting it. It is safer.

Summary

That's all for this article. I hope the content of this article can bring some help to your learning or work. If you have any questions, you can leave messages for communication. Thank you for your support of Yelling Tutorial.

You May Also Like