Sort ls command by last modified date and time


The ls command is the most widely used command to display the contents in a directory on a Linux/Mac device terminal.

If you want to sort the output of the ls command by date and time you can make use of the -t option along with the ls -l command,

% ls -lt

total 60
drwx------   1 root root 4096 Aug  7 17:28 root
drwxrwxrwt   1 root root 4096 Aug  7 17:20 tmp
drwxr-xr-x   5 root root  360 Aug  7 16:52 dev
dr-xr-xr-x  13 root root    0 Aug  7 16:52 sys
dr-xr-xr-x 180 root root    0 Aug  7 16:52 proc
drwxr-xr-x   1 root root 4096 Jul 31 17:20 etc
drwxr-xr-x   1 root root 4096 Jul 31 16:57 run
drwxr-xr-x   1 root root 4096 Jul 31 16:56 home
drwxr-xr-x   1 root root 4096 Apr  5 05:17 var
drwxr-xr-x   2 root root 4096 Apr  5 05:01 media
drwxr-xr-x   2 root root 4096 Apr  5 05:01 mnt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 opt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 srv
drwxr-xr-x   1 root root 4096 Apr  5 05:01 usr
lrwxrwxrwx   1 root root    7 Apr  5 05:01 bin -> usr/bin
lrwxrwxrwx   1 root root    7 Apr  5 05:01 lib -> usr/lib
lrwxrwxrwx   1 root root    8 Apr  5 05:01 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot

In order to display the latest files and directories first as a list make use of -r option as well along with -lt

% ls -ltr

total 60
drwxr-xr-x   2 root root 4096 Apr 15  2020 boot
lrwxrwxrwx   1 root root    8 Apr  5 05:01 sbin -> usr/sbin
lrwxrwxrwx   1 root root    7 Apr  5 05:01 lib -> usr/lib
lrwxrwxrwx   1 root root    7 Apr  5 05:01 bin -> usr/bin
drwxr-xr-x   1 root root 4096 Apr  5 05:01 usr
drwxr-xr-x   2 root root 4096 Apr  5 05:01 srv
drwxr-xr-x   2 root root 4096 Apr  5 05:01 opt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 mnt
drwxr-xr-x   2 root root 4096 Apr  5 05:01 media
drwxr-xr-x   1 root root 4096 Apr  5 05:17 var
drwxr-xr-x   1 root root 4096 Jul 31 16:56 home
drwxr-xr-x   1 root root 4096 Jul 31 16:57 run
drwxr-xr-x   1 root root 4096 Jul 31 17:20 etc
dr-xr-xr-x 181 root root    0 Aug  7 16:52 proc
dr-xr-xr-x  13 root root    0 Aug  7 16:52 sys
drwxr-xr-x   5 root root  360 Aug  7 16:52 dev
drwxrwxrwt   1 root root 4096 Aug  7 17:20 tmp
drwx------   1 root root 4096 Aug  7 17:32 root

Conclusion:

  • ls -l: l option to displays the output as a list
  • ls -lt: t option to display list in order of oldest date to lastest date and time
  • ls -ltr: tr option to display list in order of lastest date to oldest date and time
Sort ls command date and time
Sort ls command date and time


















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap