How to get total size of folders with find and du?

社会主义新天地 提交于 2019-12-29 05:03:07

问题


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

I do that : find -name bak -type d -exec du -ch '{}' \;

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

Anyway to get them ? Thanks :)


回答1:


Use xargs(1) instead of -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:

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

If your version of find supports it.




回答2:


Try du -hcs. From the manpage:

 -s, --summarize
      display only a total for each argument



回答3:


Feed du with the results of find:

du -shc $(find . -name bak -type d)



回答4:


If there are many files, using -exec ... + may be executed multiple times and you would get multiple subtotals.

An alternative is to pipe the result of find:

find . -name bak -type d -print0 | du -ch --files0-from=-


来源:https://stackoverflow.com/questions/9794791/how-to-get-total-size-of-folders-with-find-and-du

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!