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

link|improve this question

70% accept rate
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
feedback

5 Answers

up vote 5 down vote accepted

ls(1) /sort:

-S     sort by file size
link|improve this answer
feedback
$ 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
link|improve this answer
feedback

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

ls -lsh | sort -n | sed 's/^[0-9 ]* //'
link|improve this answer
feedback

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
link|improve this answer
feedback

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

du -h | sort -rh

mine is

$ sort --version
sort (GNU coreutils) 8.12
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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