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:
ls -lt
This results in output similar to the following:
$ ls -lt total 8448 drwx------+ 46 al al 1564 Oct 22 11:48 Desktop drwx------+ 25 al al 850 Oct 18 17:49 Downloads drwx------+ 224 al al 7616 Oct 15 20:13 Pictures drwxr-xr-x 31 al al 1054 Oct 15 17:09 tmp drwxr-xr-x 19 al al 646 Jul 20 19:29 Work drwxr-xr-x+ 23 al al 782 Jun 20 11:43 Sites drwxr-xr-x 9 al al 306 May 20 19:52 AlexanderConsulting drwxr-xr-x 18 al al 612 Apr 20 2009 Reference drwxr-xr-x 5 al al 170 Apr 4 2009 GarageBandProjects drwxr-xr-x 9 al al 306 Mar 19 2009 tomcat6 drwxr-xr-x 4 al al 136 Mar 12 2009 Workspaces drwx------+ 9 al al 306 Jan 2 2009 Music drwxr-xr-x 10 al al 340 Mar 9 2008 XcodeProjects drwx------+ 8 al al 272 Sep 9 2007 Movies drwxr-xr-x 11 al al 374 Aug 19 2007 AudioRecordings drwxr-xr-x 4 al al 136 Jul 19 2007 Apple Stuff drwxr-xr-x 3 al al 102 Feb 18 2007 DefaultEclipseWorkspace drwxr-xr-x 3 al al 102 Jan 14 2007 Apps -rw-r--r--@ 1 al al 7608 Apr 1 2006 MYSQL_README drwxr-xr-x+ 5 al al 170 Apr 1 2006 Public
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'.)
Here's some sample output from that command:
total 49288 -rwx------ 1 al al 25232591 Sep 11 2008 apache-cxf-2.1.2.zip drwxr-xr-x 13 al al 442 Sep 11 2008 lib drwxr-xr-x 10 al al 340 Sep 11 2008 test-clients drwxr-xr-x 9 al al 306 Sep 11 2008 src drwxr-xr-x 5 al al 170 Sep 11 2008 build drwxr-xr-x 4 al al 136 Sep 11 2008 cobertura drwxr-xr-x 3 al al 102 Sep 11 2008 sql drwxr-xr-x 3 al al 102 Sep 11 2008 src-tests drwxr-xr-x 2 al al 68 Sep 11 2008 bin drwxr-xr-x 2 al al 68 Sep 11 2008 reports drwxr-xr-x 2 al al 68 Sep 11 2008 target drwxr-xr-x 2 al al 68 Sep 11 2008 temp
Alternatively, to sort by filesize from smallest to largest, again just add the -r
option to reverse the default sort, as shown in this ls command example:
ls -lSr
Here's that command, followed by its output for the same directory shown in the previous command:
$ ls -lSr total 49288 drwxr-xr-x 2 al al 68 Sep 11 2008 temp drwxr-xr-x 2 al al 68 Sep 11 2008 target drwxr-xr-x 2 al al 68 Sep 11 2008 reports drwxr-xr-x 2 al al 68 Sep 11 2008 bin drwxr-xr-x 3 al al 102 Sep 11 2008 src-tests drwxr-xr-x 3 al al 102 Sep 11 2008 sql drwxr-xr-x 4 al al 136 Sep 11 2008 cobertura drwxr-xr-x 5 al al 170 Sep 11 2008 build drwxr-xr-x 9 al al 306 Sep 11 2008 src drwxr-xr-x 10 al al 340 Sep 11 2008 test-clients drwxr-xr-x 13 al al 442 Sep 11 2008 lib -rwx------ 1 al al 25232591 Sep 11 2008 apache-cxf-2.1.2.zip
Unix ls command - sort by filename extension
Another cool thing I didn't know is that you can also sort ls command output by filename extension. All you have to do is use the "-X" argument, as shown in this example:
ls -1X
(The arguments there are the number one (1) and the capital letter 'X'. I use the number one to get single column output, so you can see the filename extensions more easily.)
In a different directory that's full of files with different filename extensions, this command results in the following sorted output:
mission-statement.shtml navbar.shtml new.shtml sitemap.shtml siteOfDay.shtml sites.tgz CHANGELOG.txt COPYRIGHT.txt INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.txt LICENSE.txt MAINTAINERS.txt robots.txt UPGRADE.txt sitemap.xml
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:
$ ls -lX -rwxr-xr-x 1 root root 7023 Apr 6 2000 mission-statement.shtml -rw-r--r-- 1 root root 573 Sep 25 2007 navbar.shtml -rwxr-xr-x 1 root root 5430 Sep 16 22:57 sitemap.shtml -rw-r--r-- 1 root root 72 Mar 10 2001 siteOfDay.shtml -rw-r--r-- 1 root root 5900935 Sep 19 19:37 sites.tgz -rw-r--r-- 1 1080 1080 43058 Sep 16 15:34 CHANGELOG.txt -rw-r--r-- 1 1080 1080 988 Sep 14 08:50 COPYRIGHT.txt -rw-r--r-- 1 1080 1080 1308 Nov 19 2007 INSTALL.mysql.txt -rw-r--r-- 1 1080 1080 1075 Nov 26 2007 INSTALL.pgsql.txt -rw-r--r-- 1 1080 1080 15646 Jul 9 2008 INSTALL.txt -rw-r--r-- 1 1080 1080 18048 Jan 6 2009 LICENSE.txt -rw-r--r-- 1 1080 1080 1924 Apr 29 13:15 MAINTAINERS.txt -rw-r--r-- 1 1080 1080 1590 Dec 10 2008 robots.txt -rw-r--r-- 1 1080 1080 5002 Jan 4 2008 UPGRADE.txt -rw-r--r-- 1 root root 1840 May 17 11:21 sitemap.xml
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.