Sort Directories by Size in Ksh

disk-usagekshsort

I'd like to sort all the directories/files in a specific directory based on their size (using du -sh "name").

I need to apply this command to all directories in my location, then sort them based on this result.
How can I do that ?

Best Answer

With GNU sort and GNU du (which it appears you have, since you state you are using du's -h option):

du -sh -- *  | sort -rh  # Files and directories, or
du -sh -- */ | sort -rh  # Directories only

The output looks something like this:

22G     foo/
21G     bar/
5.4G    baz/
2.1G    qux/
1021M   wibble/
4.0K    wobble/
Related Question