Linux下删除大量文件


主要参考了http://www.slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux

首先建立50万个文件

➜  test    i  $(  );  text >>$i.txt;                 

1.   rm

➜  test    -f * /home/hungerr/test [yn]?: 
 -f *  .63s user .29s system % cpu  total

由于文件数量过多,rm不起作用。

2.  find

➜  test    ./ -type f -exec  ./ -type f -exec  {} \;  .86s user .13s system % cpu : total

大概43分钟,我的电脑。。。。。。边看视频边删的。

3.  find with delete

➜  test    ./ -type f - ./ -type f -delete  .43s user .21s system % cpu : total

用时9分钟。

4.  rsync

首先建立空文件夹blanktest

➜  ~   rsync -a --delete blanktest/ test/-a --delete blanktest/ test/  .59s user .86s system % cpu  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
      
➜  ~

大概用时8分钟。

6.  Perl

➜  test    -e 
 -e   .28s user .23s system % cpu  total

16s,这个应该最快了。


 

统计一下:

 命令耗费时间
rm  文件数量太多,不可用
find with -exec 50万文件耗时43分钟
find with -delete 9分钟
Perl 16s
Python 8分钟
rsync with -delete 16s

相关内容