bash 如何使用find和du获取文件夹的总大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9794791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get total size of folders with find and du?
提问by Piokaz
I'm trying to get the size of the directories named "bak" with find and du.
我正在尝试使用 find 和 du 获取名为“bak”的目录的大小。
I do that : find -name bak -type d -exec du -ch '{}' \;
我这样做: find -name bak -type d -exec du -ch '{}' \;
But it returns the size for each folder named "bak" not the total.
但它返回名为“bak”的每个文件夹的大小而不是总数。
Anyway to get them ? Thanks :)
无论如何要得到它们?谢谢 :)
回答by Carl Norum
Use xargs(1)
instead of -exec
:
使用xargs(1)
代替-exec
:
find . -name bak -type d | xargs du -ch
-exec
executes the command for each file found (check the find(1)
documentation). Piping to xargs
lets you aggregate those filenames and only run du
once. You could also do:
-exec
为找到的每个文件执行命令(检查find(1)
文档)。管道xargs
使您可以聚合这些文件名并且只运行du
一次。你也可以这样做:
find -name bak -type d -exec du -ch '{}' \; +
If your version of find
supports it.
如果您的版本find
支持它。
回答by Lars Kotthoff
Try du -hcs
. From the manpage:
试试du -hcs
。从联机帮助页:
-s, --summarize
display only a total for each argument
回答by rvrsd
Feed du with the results of find:
用 find 的结果喂给 du:
du -shc $(find . -name bak -type d)
回答by bitozoid
If there are many files, using -exec ... +
may be executed multiple timesand you would get multiple subtotals.
如果有很多文件,使用-exec ... +
可能会被执行多次,你会得到多个小计。
An alternative is to pipe the result of find:
另一种方法是通过管道传递 find 的结果:
find . -name bak -type d -print0 | du -ch --files0-from=-