Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from stackoverflow.com Sep 29 '12 at 15:21

This question came from our site for professional and enthusiast programmers.

up vote 3 down vote accepted

Sort using -h

du -h | sort -h

From sort man page

 -h, --human-numeric-sort
              compare human readable numbers (e.g., 2K 1G)
share|improve this answer
    
OS X's sort doesn't have an -h option, but you could install gsort with brew install coreutils. – user495470 Sep 29 '12 at 15:47
    
thanks for gsort – Xitrum Sep 29 '12 at 20:18

You should try the following code:

du -s * | sort -n 

This does the following

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

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

You'll get the results in kilobytes.

share|improve this answer

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

share|improve this answer
1  
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .