Sorting ls Output Based on Time and Date


You may want to list the files in a directory based on last modified time and date from the latest to the oldest, you will need to use -t

% ls -lt
drwxr-xr-x  26 root root    4096 Mar 29 13:40 mono
drwxr-xr-x   2 root root      42 Mar 29 13:40 mono-source-libs
lrwxrwxrwx   1 root root      22 Dec 21 12:55 libnss_centrifyda.so.0 -> libnss_centrifyda.so.2
drwxr-xr-x.  2 root root      62 Dec 21 12:54 security
lrwxrwxrwx   1 root root      22 Dec 21 12:54 libnss_centrifydc.so.0 -> libnss_centrifydc.so.2
lrwxrwxrwx   1 root root      28 Dec 21 12:54 libnss_centrify_logonly.so.0 -> libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  238560 Nov 17 09:13 libnss_centrifydc.so.2
-rwxr-xr-x   1 root root  140064 Nov 17 09:13 libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  159112 Nov  9 02:39 libnss_centrifyda.so.2
drwxr-xr-x   4 root root      40 Jul 25  2021 fastx
drwxr-xr-x.  2 root root     100 Dec 11  2020 modprobe.d
drwxr-xr-x   8 root root     180 Oct 26  2020 rstudio-server
.....
.....

If you want a sorting from the oldest to the latest, you use the -r option

% ls -ltr
.....
.....
-rwxr-xr-x   1 root root  159112 Nov  9 02:39 libnss_centrifyda.so.2
-rwxr-xr-x   1 root root  140064 Nov 17 09:13 libnss_centrify_logonly.so.2
-rwxr-xr-x   1 root root  238560 Nov 17 09:13 libnss_centrifydc.so.2
lrwxrwxrwx   1 root root      28 Dec 21 12:54 libnss_centrify_logonly.so.0 -> libnss_centrify_logonly.so.2
lrwxrwxrwx   1 root root      22 Dec 21 12:54 libnss_centrifydc.so.0 -> libnss_centrifydc.so.2
drwxr-xr-x.  2 root root      62 Dec 21 12:54 security
lrwxrwxrwx   1 root root      22 Dec 21 12:55 libnss_centrifyda.so.0 -> libnss_centrifyda.so.2
drwxr-xr-x   2 root root      42 Mar 29 13:40 mono-source-libs
drwxr-xr-x  26 root root    4096 Mar 29 13:40 mono

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.