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

A brief analysis of event bubbling and event capture in js

Table of contents 01-Event Bubbling 1.1- Introduc...

Analysis of the ideas of implementing vertical tables in two ways in Vue project

Problem Description In our projects, horizontal t...

Vue uses ECharts to implement line charts and pie charts

When developing a backend management project, it ...

Docker core and specific use of installation

1. What is Docker? (1) Docker is an open source t...

Node+Express test server performance

Table of contents 1 Test Environment 1.1 Server H...

Introduction to new features of ECMAscript

Table of contents 1. Default values ​​for functio...

Implementation code of html floating prompt box function

General form prompts always occupy the form space...

How to count down the date using bash

Need to know how many days there are before an im...

Steps to split and compress CSS with webpack and import it with link

Let's take a look at the code file structure ...

Vue uses WebSocket to simulate the chat function

The effect shows that two browsers simulate each ...

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

Recommended plugins and usage examples for vue unit testing

Table of contents frame First-class error reporti...

WeChat applet implements calculator function

WeChat Mini Programs are becoming more and more p...

How to operate json fields in MySQL

MySQL 5.7.8 introduced the json field. This type ...

A brief discussion on the font settings in web pages

Setting the font for the entire site has always b...