How to Sort Files by Date Using LS command in Linux

ls command sort by date

The ls command is used to list directory contents and the results can be sorted upon several criteria such as by date, alphabetical order of filenames, modification time, access time, version and file size.

In this article, I will show you how to sort files by date using ls command in Linux.

Hope you interested in reading: How to Sort all Files by Size Using ls command in Linux

1) List Files directory with Last Modified Date/Time

To list files and shows the last modified files at top, we will use -lt options with ls command.

$ ls -lt /run
output
total 24
-rw-rw-r--.  1 root utmp 2304 Sep  8 14:58 utmp
-rw-r--r--.  1 root root    4 Sep  8 12:41 dhclient-eth0.pid
drwxr-xr-x.  4 root root  100 Sep  8 03:31 lock
drwxr-xr-x.  3 root root   60 Sep  7 23:11 user
drwxr-xr-x.  7 root root  160 Aug 26 14:59 udev
drwxr-xr-x.  2 root root   60 Aug 21 13:18 tuned

2) List Files with Last Modified Date/Time (most recent at bottom)

We will use -ltr options with ls command to list files of a specific directory with recently modified files at the bottom.

$ ls -ltr /run
$ ls -ltr /run
total 13404
drwxr-xr-x 2 root        root           4096 Dec 14  2016 scripts
-rwxr-xr-x 1 root        root           4688 Dec 14  2016 perms.py
-rw-r--r-- 1 root        root           9718 Jun 23 14:47 ddagent-install.log
-rw-r--r-- 1 root        root        1457471 Jun 26 01:26 rocket.zip
drwxr-xr-x 2 root        root           4096 Jun 26 10:40 ssl-21APR2018-11JUN2020
drwxr-xr-x 6 root        root           4096 Jun 27 09:29 incubator-pagespeed-ngx-latest-stable
drwxr-xr-x 9 root        root           4096 Jun 27 09:29 nginx-1.15.0
drwxr-xr-x 3 root        root           4096 Jul  2 19:55 rocket-nginx
-rw-r--r-- 1 root        root          18186 Jul 11 13:17 memcachy.zip
-rwxr-xr-x 1 root        root       12202195 Sep  4 12:21 Linux_64bit.install
:~#

If you want to sort by directory, then by dates use

$ ls -Rltr

3) Display in Human Readable format

We will use -halt options with ls command to list files of a specific directory in human readable file sizes, long format. It uses K, M, G, and T suffixes (or no suffix for bytes)

$ ls -halt /run
  total 28K
  -rw-rw-r--.  1 root utmp 1.9K Oct 28 06:02 utmp
  drwxr-xr-x.  3 root root   60 Oct 28 06:02 user
  drwxr-xr-x.  4 root root  100 Oct 28 03:48 lock
  -rw-r--r--.  1 root root    4 Oct 28 02:50 dhclient-eth0.pid
  drwxr-xr-x.  7 root root  160 Oct 25 12:16 udev
  drwxr-xr-x. 21 root root  600 Oct 25 12:15 .

4) Find files modified in Last 10 minutes

We can get files modified in last 10 minutes with command below:

$ find . -mmin -10 -type f -exec ls -l {} +
-rw-r--r--. 1 root root 53 Nov  1 01:58 ./smart.txt
-rw-r--r--. 1 root root 15 Nov  1 02:00 ./test/file1

5) Recently modified 10 files

Let see how to check recently modified 10 files in a directory using ls commands. We will use a combination of 'ls" and 'head' command.

Below command will show modified 10 files with the recently updated file at the top

$ ls -lt /run/ | head -10
 -rw-rw-r--.  1 root utmp 1920 Oct 31 01:57 utmp
drwxr-xr-x.  3 root root   60 Oct 31 01:57 user
drwxr-xr-x. 16 root root  400 Oct 30 23:06 systemd
-rw-r--r--.  1 root root    4 Oct 30 18:42 dhclient-eth0.pid
drwxr-xr-x.  4 root root  100 Oct 30 03:06 lock
drwxr-xr-x.  7 root root  160 Oct 28 06:09 udev
-rw-------.  1 root root    3 Oct 25 12:15 syslogd.pid
drwxr-xr-x.  2 root root   60 Oct 25 12:15 tuned
-rw-r--r--.  1 root root    4 Oct 25 12:15 sshd.pid

Or

with tail combination, it shows recently updated file at the bottom.

$ ls -ltr /run/ | tail -10
drwxr-xr-x.  3 root root  100 Oct 25 12:15 NetworkManager
-rw-r--r--.  1 root root    4 Oct 25 12:15 sshd.pid
drwxr-xr-x.  2 root root   60 Oct 25 12:15 tuned
-rw-------.  1 root root    3 Oct 25 12:15 syslogd.pid
drwxr-xr-x.  7 root root  160 Oct 28 06:09 udev
drwxr-xr-x.  4 root root  100 Oct 30 03:06 lock
-rw-r--r--.  1 root root    4 Oct 30 18:42 dhclient-eth0.pid
drwxr-xr-x. 16 root root  400 Oct 30 23:06 systemd
drwxr-xr-x.  3 root root   60 Oct 31 01:57 user
-rw-rw-r--.  1 root utmp 1920 Oct 31 01:57 utmp

Thanks for reading this article and please comment below if you find any other options useful.

Read Also:

Ahmed Abdalhamid 7:09 am

About Ahmed Abdalhamid

Ahmed is communication engineer since 2001, he had a lot of skills. He has worked Linux system administrator, Python developer for 7 years and now he is Data scientist since 2015. He thinks Linux and Python are the greatest inventions of mankind.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.