Pio*_*kaz 21 bash shell debian
我正在尝试使用find和du来获取名为"bak"的目录的大小.
我这样做: find -name bak -type d -exec du -ch '{}' \;
但它返回名为"bak"的每个文件夹的大小而不是总数.
无论如何得到他们?谢谢 :)
Car*_*rum 22
使用xargs(1)而不是-exec:
find . -name bak -type d | xargs du -ch
Run Code Online (Sandbox Code Playgroud)
-exec对找到的每个文件执行命令(查看find(1)文档).管道xargs允许您聚合这些文件名并且只运行du一次.你也可以这样做:
find -name bak -type d -exec du -ch '{}' \; +
Run Code Online (Sandbox Code Playgroud)
如果你的版本find支持它.
小智 9
如果文件很多,using-exec ... +可能会执行多次,你会得到多个小计。
另一种方法是通过管道传输 find 的结果:
find . -name bak -type d -print0 | du -ch --files0-from=-
Run Code Online (Sandbox Code Playgroud)