Comparison of the efficiency of different methods of deleting files in Linux

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of files under Linux.

First create 500,000 files

$ test for i in $(seq 1 500000);do echo text >>$i.txt;done

1. rm delete

$ time rm -f *
zsh: sure you want to delete all the files in /home/hungerr/test [yn]? y
zsh: argument list too long: rm
rm -f * 3.63s user 0.29s system 98% cpu 3.985 total

rm does not work due to the large number of files.

2. Find and delete

$ time find ./ -type f -exec rm {} \;
find ./ -type f -exec rm {} \; 49.86s user 1032.13s system 41% cpu 43:19.17 total

About 43 minutes on my computer. . . . . . I deleted it while watching the video.

3. find with delete

$ time find ./ -type f -delete
find ./ -type f -delete 0.43s user 11.21s system 2% cpu 9:13.38 total

It takes 9 minutes.

4. rsync delete

# First create an empty folder blanktest
$ time rsync -a --delete blanktest/ test/
rsync -a --delete blanktest/ test/ 0.59s user 7.86s system 51% cpu 16.418 total16s

Very good and powerful.

5. Python Delete

import os
import timeit
 
def main():  
  for pathname,dirnames,filenames in os.walk('/home/username/test'):    
    for filename in filenames:      
      file = os.path.join(pathname,filename)      
      os.remove(file)     
if __name__ == '__main__':
t = timeit.Timer('main()','from __main__ import main')
print t.timeit(1) 
 1
2
$ python test.py 529.309022903

It takes about 9 minutes.

6. Perl Delete

$ time perl -e 'for(<*>){((stat)[9]<(unlink))}'
perl -e 'for(<*>){((stat)[9]<(unlink))}' 1.28s user 7.23s system 50% cpu 16.784 total16s

This should be the fastest.

7. Results:

  • rm: Too many files to use
  • Find with -exec 500,000 files took 43 minutes
  • find with -delete 9 minutes
  • Perl 16sPython 9 minutes
  • rsync with -delete 16s

Conclusion: rsync is the fastest and most convenient way to delete a large number of small files.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the problem that the space is not released after the Linux file is deleted
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]
  • How to deal with the problem that the file is deleted but the space is not released in Linux
  • Linux unlink function and how to delete files
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to delete folders, files, and decompress commands on Linux servers
  • 5 Ways to Clear or Delete Large File Contents in Linux

<<:  Understanding v-bind in vue

>>:  Detailed explanation of the use of MySQL mysqldump

Recommend

HTML background color gradient effect achieved through CSS style

Effect screenshots: Implementation code: Copy code...

How to configure Bash environment variables in Linux

Shell is a program written in C language, which i...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...

Enabling and configuring MySQL slow query log

Introduction MySQL slow query log is an important...

Tutorial on configuring and using i3 window manager in Linux

In this article, I will show you how to install a...

Example of adding multi-language function to Vue background management

Table of contents 1. First, configure the main.js...

Vue.js application performance optimization analysis + solution

Table of contents 1. Introduction 2. Why do we ne...

The use of anchor points in HTML_PowerNode Java Academy

Now let's summarize several situations of con...

Summary of web design experience and skills

■ Website theme planning Be careful not to make yo...

Introduction to the use of MySQL pt-slave-restart tool

Table of contents When setting up a MySQL master-...

Quickly solve the problem of slow Tomcat startup, super simple

Today I helped a classmate solve a problem - Tomc...

Solution to Incorrect string value in MySQL

Many friends will report the following error when...