如何使用find和du获取文件夹的总大小? [英] How to get total size of folders with find and du?

查看:236
本文介绍了如何使用find和du获取文件夹的总大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用find和du获取名为"bak"的目录的大小.

I'm trying to get the size of the directories named "bak" with find and du.

我这样做:find -name bak -type d -exec du -ch '{}' \;

但是它返回每个名为"bak"的文件夹的大小,而不是总数.

But it returns the size for each folder named "bak" not the total.

有办法得到他们吗?谢谢:)

Anyway to get them ? Thanks :)

推荐答案

使用 xargs(1) 代替-exec:

find . -name bak -type d | xargs du -ch

-exec对找到的每个文件执行命令(请查看 find(1) 文档).通过管道传输到xargs,您可以聚合这些文件名,并且仅运行一次du.您也可以这样做:

-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:

find -name bak -type d -exec du -ch '{}' \; +

如果您的find版本支持.

这篇关于如何使用find和du获取文件夹的总大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆