Linux under the correct fast delete mass file method sharing

  • 2020-05-24 06:40:43
  • OfStack

preface

Note that by "massive" in this article, I don't mean large, I mean quantity, like millions of small files in a single directory.

Recently, when optimizing the server, I found that the maildrop directory and clientmqueue directory under postfix were sending out a large number of existing files to be used in these directories ls It is foolish to order, but to execute rm * , no response, no reduction in the number of files, that is, in the mass file directory directly used rm The command to delete is invalid.

So what's the right way to do it? There are two options:

1 species:


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

The second:


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

The above two methods can successfully delete a large number of files, also very fast. But there is a better way, such as to delete the clientmqueue directory mentioned above, which is full of one-by-one emails, using 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 is to rename the directory and then use --reference Refer to the parameters to rebuild the directory, and then delete the renamed directory. The direct delete directory method is 10 cents faster. You can also leave the backup undeleted. More secure.

conclusion


Related articles: