此命令获取所有文件和大小find . -type f | xargs -I x du -s x > /tmp/asd比这个 python 脚本慢 1000 倍import osfor dirpath, dirnames, filenames in os.walk("."): for f in filenames: print(f) fp = os.path.join(dirpath, f) if os.path.isfile(fp): print(os.path.getsize(fp))我在 bash 脚本中做错了吗?目录结构大约有 500 万个文件
2 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
find可以直接-printf选择在找到的文件上显示一些数据:
%p File's name.
%s File's size in bytes.
%u File's user name, or numeric user ID if the user has no name.
%g File's group name, or numeric group ID if the group has no name.
...
因此,比出色的@Aaron 解决方案更进一步,您可以直接获取所需的信息,而无需通过xargsnor启动任何其他进程-exec:
find . -type f -printf '%p %s\n'
PIPIONE
TA贡献1829条经验 获得超9个赞
我建议使用以下内容:
find . -type f -exec stat --printf='%n %s\n' {} +
性能的主要提升来自避免为每个匹配的文件生成一个新进程find
。xargs
这可以用find
. 这是通过使用
(而不是)来完成的,这将产生尽可能少的进程(基于您的操作系统支持的最大参数数量)。find
-exec ... +
-exec ... \;
此外,我们使用更基本的stat
而不是du
查询文件的大小,这更接近您在 python 脚本中所做的。
添加回答
举报
0/150
提交
取消