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 | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | 1m |
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 .... ..
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:
Recommended readings
- See the ls command man page for more information.
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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.