linux下删除大量小文件方法汇总

除了链接http://www.jb51.net/article/46496.htm中提到的下列六种方法外,再补充一种方法,并附加测试过程:

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

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


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


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



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



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

 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

补充通过删除目录来删除目录下面的文件,然后再重建目录。

比如删除oracle里的audit目录:

rm -fr adump

测试环境:meminfo 2GB,cpu 1

下面将执行rm -fr test,用时10秒

[root@localhost ~]# echo "<< Start TEST :`date`>>";df -i;mkdir test;cd test;echo "<< Start create file :`date`>>";for i in $(seq 1 500000);do echo test >> $i.txt ;done;echo "<< End File Created :`date`>>";df -i ;cd .. ; echo "<< Start delete file :`date`>>";rm -fr test ;echo "<< End delete file :`date`>>"; df -i ;
<< Start TEST :2015年 12月 31日 星期四 02:16:04 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000   93395  926605   10% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
<< Start create file :2015年 12月 31日 星期四 02:16:04 CST>>
<< End File Created :2015年 12月 31日 星期四 02:16:44 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000  593396  426604   59% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
<< Start delete file :2015年 12月 31日 星期四 02:16:44 CST>>
<< End delete file :2015年 12月 31日 星期四 02:16:54 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000   93395  926605   10% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
[root@localhost ~]# 



下面将执行<span style="font-family: Arial, Helvetica, sans-serif;">find . -type f -exec rm {} \;</span><span style="font-family: Arial, Helvetica, sans-serif;">,用时15分钟</span>
[root@localhost ~]# echo "<< Start TEST :`date`>>";df -i;mkdir test;cd test;echo "<< Start create file :`date`>>";for i in $(seq 1 500000);do echo test >> $i.txt ;done;echo "<< End File Created :`date`>>";df -i ; echo "<< Start delete file :`date`>>";find . -type f -exec rm {} \;;cd .. ;rm -fr test ;echo "<< End delete file :`date`>>"; df -i ;
<< Start TEST :2015年 12月 31日 星期四 02:44:39 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000   93319  926681   10% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
<< Start create file :2015年 12月 31日 星期四 02:44:39 CST>>
<< End File Created :2015年 12月 31日 星期四 02:45:19 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000  593320  426680   59% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
<< Start delete file :2015年 12月 31日 星期四 02:45:19 CST>>
<< End delete file :2015年 12月 31日 星期四 03:00:34 CST>>
文件系统              Inode  已用(I)  可用(I) 已用(I)%% 挂载点
/dev/mapper/VolGroup-lv_root
                     1020000   93317  926683   10% /
tmpfs                 186844       3  186841    1% /dev/shm
/dev/sda1             128016      39  127977    1% /boot
[root@localhost ~]# 
[root@localhost ~]# 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值