linux删除大量文件的6种方法

首先建立50万个文件

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

1. rm

test  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不起作用。

2. find

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

大概43分钟。

3. find with delete

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

用时9分钟。

4. rsync

首先建立空文件夹blanktest

~  time rsync -a --delete blanktest/ test/
rsync -a --delete blanktest/ test/  0.59s user 7.86s system 51% cpu 16.418 total

16s,很好很强大。

5. Python

import os
import time
stime=time.time()
for pathname,dirnames,filenames in os.walk('/home/username/test'):
     for filename in filenames:
         file=os.path.join(pathname,filename)
         os.remove(file)
 ftime=time.time()
 print ftime-stime
~  python test.py
494.272291183

大概用时8分钟。

6. Perl

 test  time perl -e 'for(<*>){((stat)[9]<(unlink))}'
perl -e 'for(<*>){((stat)[9]<(unlink))}'  1.28s user 7.23s system 50% cpu 16.784 tota
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 4层负载均衡和7层负载均衡的区别。A、rsync的安装;软件包安装# yum install rsync 注...
    米开朗基乐阅读 3,735评论 0 1
  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 10,267评论 3 53
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,269评论 2 33
  • 测试环境: rsync-server:192.168.1.132 rsync-client:192.168.1.2...
    lailai900201阅读 2,401评论 0 10
  • 自己的事,自己担 幸福是自己创造的,痛苦也是自己创造的 快乐是自己创造的,难过也是自己创造的 恐惧是自己创造的,焦...
    昕暚阅读 995评论 1 0