Skip to content

Instantly share code, notes, and snippets.

@pharsi
Created July 8, 2023 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pharsi/2169842e3d16f2b37b37397afae587b5 to your computer and use it in GitHub Desktop.
Save pharsi/2169842e3d16f2b37b37397afae587b5 to your computer and use it in GitHub Desktop.
List and sort directories by size on disk
du -sk * | sort -rg | awk '{ numBytes = $1 * 1024; numUnits = split("B K M G T P", unit); num = numBytes; iUnit = 0; while(num >= 1024 && iUnit + 1 < numUnits) { num = num / 1024; iUnit++; } $1 = sprintf( ((num == 0) ? "%6d%s " : "%6.1f%s "), num, unit[iUnit + 1]); print $0; }'
# Thanks to answer here at https://unix.stackexchange.com/questions/4681/how-do-you-sort-du-output-by-size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment