Unix – List Files by Size – The Real Deal

“ls -ls|sort -nr|more” is close, but it actually sorts by the block size and not the actual file size. If A_file has a size of 19982, and a block size of 20, if will sort AFTER B_file with a size of 18404 and a block size of 20 as well, because the sort will go by block size and then file name.

Here’s a one-liner that will actually do the trick:

ls -l | awk ‘{ printf “%d^t%s %d %s %s %s – %5s %s^n”, $5, $1, $2, $3, $4, $6, $7, $8, $9 | “sort -nr” }’

[ Be sure to substitute an escape (i.e. backslash) for the carets (^) in the above example. They were removed by this forum’s message processor. ]

You can modify the printf parms to have the output look however you’d like.

Submitter: Larry B.

 

update:

Depending on your version of ls (I have GNU coreutils 5.94), you can get a better result with:

ls -lS

or, to list smaller files first:

ls -lrS