There are a lot of answers here, many of which are duplicates. I see three trends: piping through a second du call, using complicated shell/awk code, and using other languages.

Here is a POSIX-compliant solution using du and awk that should work on every system.

I’ve taken a slightly different approach, adding -x to ensure we stay on the same filesystem (I only ever need this operation when I’m short on disk space, so why weed out stuff I’ve mounted within this FS tree or moved and symlinked back?) and displaying constant units to make for easier visual parsing. In this case, I typically choose not to sort so I can better see the hierarchical structure.

sudo du -x | awk '
  $1 > 2^20 { s=$1; $1=""; printf "%7sG%sn", sprintf("%.2f",s/2^21), $0 }'

(Since this is in consistent units, you can then append | sort -n if you really want sorted results.)

This filters out any directory whose (cumulative) content fails to exceed 512MB and then displays sizes in gigabytes. By default, du uses a 512-byte block size (so awk’s condition of 220 blocks is 512MB and its 221 divisor converts the units to GB — we could use du -kx with $1 > 512*1024 and s/1024^2 to be more human-readable). Inside the awk condition, we set s to the size so we can remove it from the line ($0). This retains the delimiter (which is collapsed to a single space), so the final %s represents a space and then the aggregated directory’s name. %7s aligns the rounded %.2f GB size (increase to %8s if you have >10TB).

Unlike most of the solutions here, this properly supports directories with spaces in their names (though every solution, including this one, will mishandle directory names containing line breaks).

Leave a Reply

Your email address will not be published. Required fields are marked *