hualinux1.9 基础练习:如何删除900W单目录小文件

需求

有的开发代码写不好,导致单目录下缓存文件近900W个,要求删除这900W个文件,怎搞?

分析

如果是使用rm -rf 肯定不好,会卡死,提示参数过长,看来是把文件名当成参数了。顶多就80W个,已经很不错了。明显满足不了要求

可以用rsync同步清空,rsync统计文件名不会全部写在内存中了,超过一定数据会放在磁盘里,所以适合删除文件数比较多的情况

操作

[[email protected]]$ ls -1f |wc -l

8767765

[root@vm6 tools]# cd /disk1/tools

[root@vm6 tools]# time rsync --delete-before -d --progress --stats empty/ Temp.old/

...

Number of files: 1

Number of files transferred: 0

Total file size: 0 bytes

Total transferred file size: 0 bytes

Literal data: 0 bytes

Matched data: 0 bytes

File list size: 15

File list generation time: 0.001 seconds

File list transfer time: 0.000 seconds

Total bytes sent: 22

Total bytes received: 12

sent 22 bytes  received 12 bytes  0.00 bytes/sec

total size is 0  speedup is 0.00

rsync warning: some files vanished before they could be transferred (code 24) at main.c(1039) [sender=3.0.6]

real 753m37.972s(约12.5小时)

user 1m26.525s

sys 11m37.410s

可见统计和删除一共花了12.5小时,服务器是阿里云4核8G的,文件都是10多k-几十k

总结:

不要在一个目录中放大量的(通过超过200W的文件)文件,这样会对系统性能有影响,得细分成类,这样能提高性能也方便维护。

原创文章 57 获赞 2 访问量 3046

猜你喜欢

转载自blog.csdn.net/hualinux/article/details/105863110
1.9