温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux删除大量文件方法有哪些

发布时间:2021-07-26 11:51:45 来源:亿速云 阅读:165 作者:chen 栏目:开发技术

本篇内容主要讲解“linux删除大量文件方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“linux删除大量文件方法有哪些”吧!

首先建立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 total

到此,相信大家对“linux删除大量文件方法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI