网站首页 > 技术文章 正文
Linux 下海量文件删除方法效率对比,本次参赛选手一共6位,分别是:rm、find、find with delete、rsync、Python、Perl.
首先建立50万个文件
$ test for i in $(seq 1 500000);do echo text >>$i.txt;done
- 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不起作用。
- 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分钟,我的电脑。。。。。。边看视频边删的。
- 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分钟。
- rsync 删除
# 首先建立空文件夹blanktest
$ time rsync -a --delete blanktest/ test/
rsync -a --delete blanktest/ test/ 0.59s user 7.86s system 51% cpu 16.418 total16s,很好很强大。
- 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)
$ python test.py
529.309022903
# 大概用时9分钟。
- 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,这个应该最快了。
测试结果
- rm:文件数量太多,不可用
- find with -exec 50万文件耗时43分钟
- find with -delete 9分钟
- Perl 16sPython 9分钟
- rsync with -delete 16s
结论:删除大量小文件rsync最快,最方便
猜你喜欢
- 2025-06-28 python文件操作常用方法整理(python文件操作总结)
- 2025-06-28 Python3查看文件是否存在,以及读、写与执行的属性
- 2025-06-28 Python 中的前缀删除操作全指南(python删除后缀)
- 2025-06-28 Python教程(18)——python文件操作详解
- 2025-06-28 你的手机是不是经常提示存储空间不足?Python帮你清理重复文件
- 2025-06-28 用于清理数据的 5 个简单有效 Python 脚本
- 2025-06-28 「万能Python」-15-文件处理(关于python对文件的处理)
- 2025-06-28 Linux系统自带Python2&yum的卸载及重装
- 2025-06-28 python应用-shutil详解文件复制/移动/删除功能
- 2025-06-28 Python处理文本的25个经典操作(python文本分析:从入门到精通)
- 272℃Python短文,Python中的嵌套条件语句(六)
- 271℃python笔记:for循环嵌套。end=""的作用,图形打印
- 269℃PythonNet:实现Python与.Net代码相互调用!
- 264℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 264℃Python实现字符串小写转大写并写入文件
- 123℃原来2025是完美的平方年,一起探索六种平方的算吧
- 104℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 99℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python数据分析实战-dataframe分组提取每一组的首条记录
- 如何使用Python将多个excel文件数据快速汇总?
- 「Python数据分析」Pandas进阶,使用groupby分组聚合数据(二)
- 还在熬夜合并30个Excel 3个案例,带你用Python玩转Excel高阶操作
- python数据分析实战:pandas分组聚合-自定义聚合函数
- Python 知识点 #31 - 分组和聚(python分层聚类)
- 人生苦短,自学 python——pandas 的分组操作
- 利用Python进行数据分组/数据透视表
- 超实用!用Python快速实现数据分组统计与透视表
- Python 之 Pandas:数据分组聚合统计的魔法秘籍
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)