Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

Sorry should have mentioned this before, the operating system is AIX 6.1 with ksh shell.

Thank you.

share|improve this question
add comment

3 Answers

up vote 10 down vote accepted

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
share|improve this answer
2  
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
1  
Updated. Thanks for the tip ^^ –  rahmu Oct 11 '11 at 22:05
    
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? –  user815423426 Jul 19 '13 at 22:41
    
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
add comment

If you have GNU utilities, try

find . -type f -print0 | du --files0-from=-
share|improve this answer
    
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
add comment

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.

share|improve this answer
    
The then I usually add is worth of gold :-) –  Radek Oct 2 '12 at 0:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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