51

How can I get the size of all files and all files in its subdirectories using the du command.

I am trying the following command to get the size of all files (and files in subdirectories)

find . -type f | du -a

But this prints out the folder sizes as well. How can I get a listing of sizes of all files and files in subdirectories? I also tried the exec flag but I am not sure how to pipe the output into another command after it executes the results of find into du.

The operating system is AIX 6.1 with ksh shell.

57

I usually use the -exec utility. Like this:

find . -type f -exec du -a {} +

I tried it both on bash and ksh with GNU find. I never tried AIX, but I'm sure your version of find has some -exec syntax.

The following snippet sorts the list, largest first:

find . -type f -exec du -a {} + | sort -n -r | less
  • 3
    I'd go with this answer if you don't have access to find -print0 or other GNU features. If available, replacing \; with \+ will result in fewer invocations of du and thus better performance. – jw013 Oct 11 '11 at 22:02
  • Thanks, this works out great especially since du offers a flag for size in different units. – Shardul Upadhyay Oct 12 '11 at 12:59
  • I could not find information on the + option. Is that an option for du or for find ? And why does it result in less calls? – Amelio Vazquez-Reina Jul 19 '13 at 22:41
  • 1
    It's a standard option of find. It specifies to exec the command (in our case du) only once, with all the results of find given as successive arguments to the command. – rahmu Jul 20 '13 at 2:25
  • wtf, why isn't there a command like du -f --threshold=1G – Alexander Mills Dec 22 '18 at 3:41
16

If you have GNU utilities, try

find . -type f -print0 | du --files0-from=-
  • The command is failing saying print0 is not a valid command and that last minus was not a recognized flag. I don't think this approach will work because man du doesn't list a files or from flag. – Shardul Upadhyay Oct 11 '11 at 20:00
  • You should add your operating system as a tag to the question. I assumed you had GNU but forgot to mention that. – jw013 Oct 11 '11 at 20:04
  • 1
    Have an upvote on me! Your particular solution works with du -ch to get a grand total of matching files: find . -name 'blah blah.*' -print0 | du --files0-from=- -ch – Michael Goldshteyn Jul 15 '18 at 14:05
9

I generally use:

find . -type f -print0 | xargs -r0 du -a

Xargs usually calls the command, even if there are no arguments passed; xargs du </dev/null will still be called, xargs -r du </dev/null will not call du. The -0 argument looks for null-terminated strings instead of newline terminated.

Then I usually add | awk '{sum+=$1} END {print sum}' on the end to get the total.

  • 2
    The then I usually add is worth of gold :-) – Radek Oct 2 '12 at 0:56
2

Here's a version with long parameter names and human sorting.

find . -type f -exec du --human {} + | sort --human --reverse | head

I also saw no need for -a/--all to be passed to du.

-2

specific files in current dir

du -ch files*

is shorter and works for me

du -sh .

for current dir and all files in sub

du (GNU coreutils) 8.30

  • 1
    (1) What do you mean by du -ch files*?  Suppose the current directory contains ant, apple, banana, bat, cat, corn, date, and dog, where the animal names are subdirectories and the fruit / vegetable names are files — what command would you use to get just the files?  Or is that not what you meant? (2) du -sh . will report only a grand total of everything in and under the current directory, and not the files themselves at all.  This is pretty much the exact opposite of what the question asked for. – G-Man Says 'Reinstate Monica' Jan 25 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.