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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
If you have GNU utilities, try
find . -type f -print0 | du --files0-from=-
Method 3
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.
Method 4
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.
Method 5
For completeness: The accepted answer does what the question asks for in that it uses du specifically, but please note that du will print out the disk usage (which depends on the block size of the partition where the file is stored), which is not generally the same as the file size (which is constant, no matter where the file is stored). An alternative that prints the number of bytes in the file instead of the disk usage would be
find . -type f -printf '%s %fn' (or find . -type f -printf '%s %fn' | sort -k2 to sort by name).
Method 6
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0