I'm using mac and I use sudo du -h to show all sub directories in a parent directory with their size. Is there anyway i can use to sort the result by size?

I tried using sudo du -h | sort but the result was organize in a strange way to me (Eg. 1K - 2k - 1M - 2M - 2K...)

Thank you very much.


Sort using -h

du -h | sort -h

From sort man page

 -h, --human-numeric-sort
              compare human readable numbers (e.g., 2K 1G)
  • OS X's sort doesn't have an -h option, but you could install gsort with brew install coreutils. – Lri Sep 29 '12 at 15:47

You should try the following code:

du -s * | sort -n 

This does the following

    -s, --summarize
          display only a total for each argument

    -n, --numeric-sort
          compare according to string numerical value

You'll get the results in kilobytes.


here one way available for your question

du -h | sort -n

in du command -h is for human readable format

sort command -n for numeric sort

  • 2
    sort -n do not sort using the K M G notation. (12k will be found after 9M), you need to sort using -h – Tom Ahh Sep 29 '12 at 12:50
  • I prefer du -s | sort -n instead, because it gives summary instead and K M G issue won't be there. – Arda Aug 6 '18 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.