Piokaz
Piokaz

Reputation: 374

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

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

Upvotes: 30

Views: 48357

Answers (4)

rvrsd
rvrsd

Reputation: 131

Feed du with the results of find:

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

Upvotes: 3

bitozoid
bitozoid

Reputation: 146

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

Upvotes: 11

Carl Norum
Carl Norum

Reputation: 224944

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.

Upvotes: 42

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

Try du -hcs. From the manpage:

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

Upvotes: 4

Related Questions