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

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


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.


Try du -hcs. From the manpage:

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


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