为什么“找到 . -名称 *.txt | xargs du -hc”给出多个总计?
我有一大堆目录,我正在尝试计算其中数百个 .txt 文件的总大小。我尝试了这个,大部分都有效:
find . -name *.txt | xargs du -hc
但是最后我没有给我一个总数,而是得到了几个。我的猜测是,管道一次只会传递 find 输出的这么多行,而 du 只是对每个批次进行操作。有办法解决这个问题吗?
谢谢! 亚历克斯
I have a large set of directories for which I'm trying to calculate the sum total size of several hundred .txt files. I tried this, which mostly works:
find . -name *.txt | xargs du -hc
But instead of giving me one total at the end, I get several. My guess is that the pipe will only pass on so many lines of find's output at a time, and du just operates on each batch as it comes. Is there a way around this?
Thanks!
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一种替代解决方案是使用 awk:
One alternate solution is to use awk:
对 du 使用 --files0-from 选项怎么样?您必须正确生成以空结尾的文件输出:
在我的系统上正常工作。
How about using the --files0-from option to du? You'd have to generate the null-terminated file output appropriately:
works correctly on my system.
如果您想搜索多个不同的扩展名,最好这样做:
and if you want to have several different extensions to search for it's best to do:
xargs 程序将事情分成批次,以考虑由于 unix 命令行的最大长度而产生的限制。它仍然比一次运行一个子命令更有效,但是,对于一长串输入,它将运行该命令足够多次,每次“运行”足够短,不会导致问题。
因此,您可能会看到 xargs 需要运行的每个“批次”有一个输出行。
因为您可能会发现它有用/有趣,所以可以在此处在线找到手册页: http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs
另一件需要注意的事情(这可能是您帖子中的拼写错误或我的误解)是您有“*. txt”未转义/引用。即,您有
可能想要的地方。
区别在于命令行可能会将 * 扩展到匹配的文件名列表中...而不是将 * 传递到 find 中,后者会将其用作模式。
The xargs program breaks things up into batches, to account for the limits due to the maximum length of a unix command line. It's still more efficient than running your subcommand one at a time but, for a long list of inputs, it will run the command enough times that each "run" is short enough that it won't cause issues.
Because of this, you're likely seeing one output line per "batch" that xargs needs to run.
Because you may find it useful/interesting, the man page can be found online here: http://unixhelp.ed.ac.uk/CGI/man-cgi?xargs
One other thing to note (and this may be a typo in your post or my misunderstanding) is that you have the "*.txt" unescaped/quoted. Ie, you have
where you probably want
The difference being that the command line may be expanding the * into the list of filenames that match... rather than passing the * into find, which will use it as a pattern.
另一个简单的解决方案:
Another simple solution:
一种替代解决方案是使用 bash
for
循环:当您需要更多地控制循环中发生的情况时,这非常有用。
One alternate solution is to use bash
for
loop:This is good for when you need more control of what happens in the loop.
xargs 将其输入分解为合理大小的块 - 您看到的是每个块的总计。检查 xargs 的手册页,了解配置其输入处理的方法。
xargs busts its input into reasonable-sized chunks - what you're seeing are totals for each of those chunks. Check the man page for xargs on ways to configure its handling of input.