Linux Redhat/Centos/Debian: How to list files sorted by date/time ?
In this article, we will look how to list files sorted by date or time in Linux.
This is one of the very basic tip which you should know while working on linux terminal.
To sort files by time, we can use the switch “-t” with ls command.
I. Sort files by time: Most recent files on top
## -t shows most recent files on top [root@ngelinux ~]# ls -lt total 16 drwxr-xr-x. 3 root root 53 Apr 2 16:12 testdir -rw-r--r--. 1 root root 6 Mar 14 20:20 testfile -rw-r--r--. 1 root root 411 Mar 14 12:26 id_rsa.pub -rw-r--r--. 1 root root 1968 Mar 13 20:56 initial-setup-ks.cfg -rw-------. 1 root root 1920 Mar 13 19:38 anaconda-ks.cfg
II. List sorted by date in reverse order: Recent files at bottom of the list
[root@ngelinux ~]# ls -ltr total 16 -rw-------. 1 root root 1920 Mar 13 19:38 anaconda-ks.cfg -rw-r--r--. 1 root root 1968 Mar 13 20:56 initial-setup-ks.cfg -rw-r--r--. 1 root root 411 Mar 14 12:26 id_rsa.pub -rw-r--r--. 1 root root 6 Mar 14 20:20 testfile drwxr-xr-x. 3 root root 53 Apr 2 16:12 testdir [root@ngelinux ~]#
III. Make the listing more human readable.
[root@ngelinux ~]# ls -ltrh total 16K -rw-------. 1 root root 1.9K Mar 13 19:38 anaconda-ks.cfg -rw-r--r--. 1 root root 2.0K Mar 13 20:56 initial-setup-ks.cfg -rw-r--r--. 1 root root 411 Mar 14 12:26 id_rsa.pub -rw-r--r--. 1 root root 6 Mar 14 20:20 testfile drwxr-xr-x. 3 root root 53 Apr 2 16:12 testdir [root@ngelinux ~]#
IV. List only 10 Files recently modified.
[root@ngelinux ~]# ls -ltr /tmp/ | tail drwx------. 3 root root 17 Apr 1 14:43 systemd-private-cfbf76e2384440e38bebb7d8c6d61f9c-fwupd.service-2XY16z drwx------. 2 saket saket 6 Apr 1 14:52 tracker-extract-files.1000 drwx------. 3 root root 17 Apr 2 13:45 systemd-private-df394d6313b24205bd1bb698d415a615-rtkit-daemon.service-LGkFdb drwx------. 3 root root 17 Apr 2 13:45 systemd-private-df394d6313b24205bd1bb698d415a615-chronyd.service-RsVXSK drwx------. 3 root root 17 Apr 2 13:46 systemd-private-df394d6313b24205bd1bb698d415a615-cups.service-uNZxqv drwx------. 3 root root 17 Apr 2 13:47 systemd-private-df394d6313b24205bd1bb698d415a615-bolt.service-06XLh9 drwx------. 3 root root 17 Apr 2 13:47 systemd-private-df394d6313b24205bd1bb698d415a615-colord.service-7tsBn1 drwx------. 2 saket saket 24 Apr 2 14:23 ssh-L5TJApfneTT0 drwx------. 3 root root 17 Apr 2 14:24 systemd-private-df394d6313b24205bd1bb698d415a615-fwupd.service-FBKi0P drwxr-xr-x. 2 root root 6 Apr 2 16:15 Sample Directory [root@ngelinux ~]#