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

Implementation of HTML to PDF screenshot saving function

Using Technology itext.jar: Convert byte file inp...

How to implement hot deployment and hot start in Eclipse/tomcat

1. Hot deployment: It means redeploying the entir...

Three notification bar scrolling effects implemented with pure CSS

Preface The notification bar component is a relat...

Zabbix monitoring solution - the latest official version 4.4 [recommended]

Zabbix 2019/10/12 Chenxin refer to https://www.za...

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

Click the toggle button in Vue to enable the button and then disable it

The implementation method is divided into three s...

MySQL table auto-increment id overflow fault review solution

Problem: The overflow of the auto-increment ID in...

How to use Linux paste command

01. Command Overview The paste command will merge...

CSS3 creates web animation to achieve bouncing ball effect

Basic preparation For this implementation, we nee...

Linux type version memory disk query command introduction

1. First, let’s have a general introduction to th...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

JavaScript Design Pattern Command Pattern

The command pattern is a behavioral design patter...

Advanced and summary of commonly used sql statements in MySQL database

This article uses examples to describe the common...