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

by on August 25, 2013 · 1 comment· LAST UPDATED August 25, 2013

in , , ,

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?

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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time1m
In other words, last downloaded file can be displayed using the following command. Open the Terminal and type the following command.

Syntax

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 ImageOptim.app
-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 english-68.zip
-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 game.English.srt
-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
....
..

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

Recommended readings
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 1 comment… read it below or add one }

1 Folkert August 30, 2013 at 8:14 am

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.

Reply

Leave a Comment

Tagged as: ,

Previous Faq:

Next Faq: