It is true that it is faster in the second run, but for a deep directory structure, it is still much slower than using only one run for the du command.
I have used a command similar to #2 for a long time but was bothered about the slow speed.
Well, yesterday I played around with awk which until yesterday has been black magic to me. I have used it several times in snippets found on the net without really understanding what it did, and how. But finally I saw the light!
So I concocted this command which works well for me and only runs du once:
Code:
du --max-depth=0 -k * | sort -nr | awk '{ if($1>=1024*1024) {size=$1/1024/1024; unit="G"} else if($1>=1024) {size=$1/1024; unit="M"} else {size=$1; unit="K"}; if(size<10) format="%.1f%s"; else format="%.0f%s"; res=sprintf(format,size,unit); printf "%-8s %s\n",res,$2 }'
It works by NOT having du output data in human readable format, but instead letting awk prettify the output.
Hope someone will find it useful. It took several hours to get it right, but now I master awk
Bookmarks