How to sort Linux ls command file output

By Alvin Alexander. Last updated: Aug 31, 2011

A couple of days ago I was asked how to sort the output from the Unix and Linux ls command. Off the top of my head I knew how to sort the ls output by file modification time, and also knew how to sort ls with the Linux sort command, but I didn't realize there were other cool file sorting options available until I looked them up.

In this short tutorial I'll demonstrate the Unix/Linux ls command file sorting options I just learned.

Sort Linux ls output by file modification time

To help us get rolling, here are two quick examples that show how to sort ls command output by file modification time.

First, to sort the ls command output by file modification time, in order from the most recently-modified file (newest) to the oldest, use this ls command:

In this ls command, the -t argument to the ls command gives us the sorting by file modification time, and the -l argument gives us this long/wide output.

To do the opposite, and sort the ls output by modification time from oldest to newest, just add the -r argument to the ls command, like this:

ls -ltr

Sort Unix ls command output by file size

What I just learned is that you can sort the ls command output by file size without piping the ls output into the Linux sort command. All you have to do is use the -S argument (that's an uppercase letter 'S') to your Unix or Linux ls command, like this:

ls -lS

(Note that those last two characters are a lowercase letter 'L' and and uppercase letter 'S'.)

To show that there's nothing magical about the number one in that previous command, here's a similar command, where I combine the 'X' with the lowercase letter 'L' to get long ls output, sorted by filename extension. Here's this ls command, followed by its output, in that same directory:

How to reverse ls command output

Just to emphasize the point, you can reverse the output from any ls command with the "-r" argument.

Other Unix/Linux ls file sort options

There are more ways to sort the ls command output, but I think those are the most common methods. But to be a little more complete, here is a list of ls sorting options I just pulled from the CentOS Linux ls command man page:

-c with -lt: sort by, and show, ctime (time of last modification of file
status information)
with -l: show ctime and sort by name otherwise: sort by ctime
-f do not sort, enable -aU, disable -lst
-r, --reverse
reverse order while sorting
-t sort by modification time
-u with -lt: sort by, and show, access time
with -l: show access time and sort by name
otherwise: sort by access time
-U do not sort; list entries in directory order.
In combination with one_per_line format '-1',
it will show files immediately and it has no
memory limitations.
-v sort by version
--sort=WORD
extension -X, none -U, size -S, time -t, version -v, status -c, time -t, atime -u, access -u, use -u

Mac OS X ls command difference

One quick note: I just tried some of these commands on Mac OS X 10.5.x, and the -X option does not work. All the other ls command sorting options worked as shown.