Linux删除文件不同方法效率对比

作者:邯郸-小刀 时间:2021-06-27 06:11:13 

测试一下Linux下面删除大量文件的效率。

首先建立50万个文件

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

1. rm删除

$ 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删除

$ 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

$ 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 total16s

很好很强大。

5. Python删除


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

大概用时9分钟。

6. Perl删除

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

这个应该最快了。

7、结果:

  • rm:文件数量太多,不可用

  • find with -exec 50万文件耗时43分钟

  • find with -delete 9分钟

  • Perl 16sPython 9分钟

  • rsync with -delete 16s

结论:删除大量小文件rsync最快,最方便。

来源:https://www.cnblogs.com/xzlive/p/12623258.html

标签:Linux,删除,文件,方法,效率
0
投稿

猜你喜欢

  • 国外著名英文搜索引擎大全

    2007-09-28 18:46:00
  • 谷歌明年开启图片广告 将获近10亿美元收入

    2008-06-25 13:25:00
  • 10种提高WordPress访问速度的方法

    2010-03-22 10:01:00
  • FTP服务器架设的三方面安全因素

    2009-02-10 16:01:00
  • 如何选择免费虚拟主机及其五点注意事项

    2008-11-27 10:24:00
  • Win2K Internet服务器安全构建指南(五)

    2007-11-01 14:22:00
  • 在Ubuntu上搭建一个基于webrtc的多人视频聊天服务实例代码详解

    2023-11-04 23:53:07
  • 二十八个让关键词排名明显改观的优化技巧

    2008-12-02 10:52:00
  • 百度欲在深设华南总部 作为拓展业务基地

    2009-11-09 08:52:00
  • 关闭selinux(防火墙)方法分享

    2022-11-03 20:49:35
  • 解读apache的配置文件http.conf

    2008-01-08 19:32:00
  • 湖南卫视进军游戏领域推出网页游戏

    2009-10-14 16:35:00
  • inux下gettimeofday函数windows替换方法(详解)

    2023-06-11 23:19:57
  • 美国各大主机空间特点综述(3)

    2010-03-17 09:26:00
  • 外部链接优化 提交到DMOZ以及链接诱饵

    2009-03-09 11:16:00
  • 如何让SupeSite7.0首页显示全部的频道分类

    2009-06-19 16:54:00
  • VMware EXSI为虚拟机centos安装VMware Tools

    2023-11-04 06:04:36
  • 搜索引擎优化策略:关键字密度

    2008-12-12 13:43:00
  • typecho首页和内容页的Title分别定义

    2010-03-24 12:10:00
  • 主题与关系并举提升网络社区的价值

    2009-02-15 09:51:00
  • asp之家 网站运营 m.aspxhome.com