Friday, July 18, 2008

Sorting Files By Size

In case anybody was wondering, I'm still alive. I've been inordinately busy lately and have been spending the majority of my free time online re-learning Ruby on Rails. I've also been digging into multi-master and circular replication schemes for MySQL and MySQL proxy. I plan on sharing some of the things I've learned sometime in the near future. Anyway, here's a handy tip that I just used this morning.

If you want to sort all the files in a directory by size, you can do the following:

ls -lsR | sort -nr

The sort command will order by the number of blocks in each file. The biggest files are at the top.


Steve Laniel said...

I just use 'ls -rS' here. No?

Anonymous said...

I often do:

$ ls -l | sort -k5,5 -n

Whispers in your ear said...

As Steve writes, the -S option is the key. These are all good:

ls -S
ls -sS
ls -lS

They give just the filenames, just file size and name, and the full long listing respectively, with the biggest file at the top.

I often combine it with head:

ls -lS | head
ls -lS | head -20