Sunday, April 17, 2005

ls sort files

The "ls" file/directory listing tool doesn't have built-in support for sorting files according to file size. But by piping the output to "sort", this can be done, e.g.

ls -al | sort +4n

Will sort by size from the smallest file to the largest. The following will display files from largest to smallest:

ls -al | sort +4nr

2 comments:

Anonymous said...

well I'm running RH5 and your sort command doesn't work for me:

Firstly, when I ls in long format, my file size is column 5, not 4.

Secondly, when I paste your command to my console, system responds:

sort: open failed: +4n: No such file or directory

However, if I use the -k option and use column 5, I can sort files by size. This works for me :

ls -l | sort -k5n

Anonymous said...

It seems that sort is different on different Linux platforms.

For OpenSUSE 11.1,

...|sort +4n...
does the trick for me, however on Ubuntu 8.10,

...|sort -k4n...
is the right choice.

Cheers,
Lx