Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

how can I display the files in a unix directory sorted by their human readable size, going from largest to smallest?

I tried:

du -h | sort -V -k 1 

but it does not seem to work. thanks

share|improve this question
    
Can you please clarify if you are expecting the subdirectory sizes to appear in the output too, and also if you are looking for the apparent size of the files or the actual size they use on disk ? –  jlliagre Dec 17 '11 at 13:42
add comment

5 Answers

up vote 8 down vote accepted

ls(1) /sort:

-S     sort by file size
share|improve this answer
add comment
$ ls -lhS

-l     use a long listing format
-h     with -l, print sizes in human readable format (e.g., 1K 234M 2G)
-S     sort by file size
share|improve this answer
add comment

Unlike ls -S, this will properly handle sparse files:

ls -lsh | sort -n | sed 's/^[0-9 ]* //'
share|improve this answer
add comment

I got this to work for me:

ls -l | sort -g -k 5 -r

Which (I just figured-out) is the same as:

ls -lS
share|improve this answer
add comment

If you have the appropriate sort version you may simply use:

du -h | sort -rh

mine is

$ sort --version
sort (GNU coreutils) 8.12
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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