Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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.

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
up vote 15 down vote accepted

ls(1) /sort:

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

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

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

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

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

ls -S wasn't an option on the OS for me. The following worked:
ls -l | sort -k 5nr
They "key" was to specify the column to sort (get it, the "key"). Above I'm specifying -k 5nr meaning sort on 5th column which is size (5) evaluated as a number (n) in descending order (n)

Reference sort documentation for more information

share|improve this answer

du -ha | sort -h

du : estimate file disk usage.

-h : for human
-a : all files

sort : sort lines of text.

-h : for human

man du; man sort for more. It works for me on ubuntu v15.

share|improve this answer

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.