Linux / Unix: Sort ls Command Output By Last Modified Date and Time

last updated in Categories , , ,

I download lots of files in ~/Downloads/ folder in Ubuntu Linux and OS X desktop. How do I show last downloaded file first using the ls command? How do I sort the output of ls command by last modified date?

You need to pass the -t option to the ls command. The -t option sort by time modified i.e. most recently modified first before sorting the operands by lexicographical order. In other words, last downloaded file can be displayed using the following command. Open the Terminal application and type the following command.



The syntax is:
ls -t
ls -lt | less
ls -lt ~/Downloads/ | less

Sample outputs:

total 60754328
drwxr-xr-x  3 vivek  staff         102 Aug 25 13:18
-rw-r--r--@ 1 vivek  staff   301746331 Aug 25 01:25 data-db2-sample.rar
-rw-r--r--@ 1 vivek  staff     1727030 Aug 25 01:14 testdisk-6.14.mac_intel.tar.tar.bz2
-rw-r--r--@ 1 vivek  staff       23850 Aug 24 22:36
-rw-r--r--@ 1 vivek  staff       72488 Aug 24 22:05 36363537dkgpd.pdf
drwxr-xr-x@ 3 vivek  staff         170 Aug 24 19:58 backups
drwxr-xr-x@ 4 vivek  staff         306 Aug 24 19:56 tarballs
-rw-------@ 1 vivek  staff       39748 Aug 24 13:22 Account-xyz.pdf
-rw-------@ 1 vivek  staff       35583 Aug 24 13:21 Portfolio-Update-FY13-14.pdf
-rw-------@ 1 vivek  staff      141695 Aug 24 01:26 13290.pdf
drwxr-xr-x@ 2 vivek  staff         136 Aug 24 00:32 hd-video-raw-files
-rw-r--r--@ 1 vivek  staff  1359349025 Aug 23 21:04 youtube-sample.mp4
drwxr-xr-x@ 2 vivek  staff         170 Aug 23 21:03 delme
-rw-r--r--@ 1 vivek  staff      120587 Aug 23 19:00 Screenshot-System-Monitor.png
-rw-r--r--@ 1 vivek  staff       23301 Aug 22 13:21 sad-tux.png

Pass the -r option to reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size), enter:
ls -tr
ls -ltr | less
ls -ltr ~/Downloads/ | less

Sample outputs:

total 60754328
-rwxr-xr-x@ 1 vivek  staff      115262 Jan  1  1970 P4.pdf
-rw-r--r--  1 vivek  staff      135734 Sep 16  2011 game.idx
-rw-r--r--  1 vivek  staff       51111 Sep 16  2011
-rwxr-xr-x@ 1 vivek  staff      331201 Sep  6  2012 Portfolio_Recommendations_Comprehensive_Sep12.pdf
-rw-r--r--  1 vivek  staff   301746245 Sep 25  2012 hp-ux-to-rhel6-guide.pdf
-rwxr-xr-x@ 1 vivek  staff       13350 Mar 13 16:44 DC-location-Towns.xlsx
-rw-r--r--@ 1 vivek  staff   574423040 Jun 10 16:33 data-center-opening.avi
-rw-r--r--@ 1 vivek  staff     2375468 Jun 15 05:04 backups.rar
-rw-r--r--  1 vivek  staff     2631709 Jun 15 05:07 last-good-know-backups.pdf

Sort by modification time, newest first and other options

Make sure you pass the -A (list all entries except for . and ..) or -a (include directory entries whose names begin with a dot) option to see hidden files:
ls -Altr ~/Downloads/ | less
ls -alt ~/Downloads/ | less

The following option turned on human readable output:
$ ls -halt
$ ls -halt | more

Sample outputs:

Fig.01: Print human readable sizes when used with the -l/-s option
Fig.01: Print human readable sizes when used with the -l/-s option

Recommended readings
  • See the ls command man page for more information.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

  1. Just use

    ls -t

    or (equivalently)

    ls –sort=time

    This will the most recently modified files (and other file system objects) first. You may want to add a -l switch for details on the files. Pipe the output through `tac` to reverse the order. Select the newest (oldest) files by piping the output through `head`. Example:

    ls -lt | head -n 3 # displays the three most recently modified files.

    Still, have a question? Get help on our forum!