ls command to sort by date


For most Unix-like systems, like macOS the default sorting order when the ls command is used is alphabetical by file name, i.e. the files are listed in ascending order, from A to Z, and directories are listed first, followed by files.

At times you would like to sort the list by date, especially to know if some files were recently added or modified.

To sort the list by date make use of the -t flag/option.


Example:
% ls -lt          
total 16
-rw-r--r--    1 c2ctech  staff     0 May  7 15:56 myfile.txt
drwx------@  10 c2ctech  staff   320 May  7 15:42 Desktop
drwx------+ 120 c2ctech  staff  3840 May  6 17:15 Downloads
drwx------@  93 c2ctech  staff  2976 May  6 00:46 Library
drwx------+   6 c2ctech  staff   192 May  5 22:59 Pictures
-rw-r--r--@   1 c2ctech  staff   433 May  3 17:33 gif-gen.py
drwxr-xr-x@   3 c2ctech  staff    96 Apr 25 14:02 Postman
drwx------+   7 c2ctech  staff   224 Apr 22 20:16 Documents
drwxr-xr-x@   3 c2ctech  staff    96 Apr 19 20:09 AndroidStudioProjects
-rw-r--r--    1 c2ctech  staff    29 Apr 18 19:33 vimrc
drwxr-xr-x    5 c2ctech  staff   160 Apr 16 10:31 AI
drwx------@   3 c2ctech  staff    96 Apr 13 21:51 Applications
drwxr-xr-x   25 c2ctech  staff   800 Apr  5 19:34 anaconda3
drwxr-xr-x@   5 c2ctech  staff   160 Mar 24 14:09 IdeaProjects
drwx------    4 c2ctech  staff   128 Mar 16 13:09 Movies
drwx------    5 c2ctech  staff   160 Mar 16 13:07 iCloud Drive (Archive)
drwx------+   4 c2ctech  staff   128 Mar 16 13:06 Music
drwxr-xr-x+   4 c2ctech  staff   128 Mar 16 13:03 Public

As you may see when I ran the ls -lt command in my current directory which is home (~) gets sorted by modification time, with the most recently modified files and directories first

The next question which is obvious, is how to sort in the reverse order, all you need to do is add -r flag to the -t

% ls -ltr
total 16
drwxr-xr-x+   4 c2ctech  staff   128 Mar 16 13:03 Public
drwx------+   4 c2ctech  staff   128 Mar 16 13:06 Music
drwx------    5 c2ctech  staff   160 Mar 16 13:07 iCloud Drive (Archive)
drwx------    4 c2ctech  staff   128 Mar 16 13:09 Movies
lrwxr-xr-x    1 c2ctech  staff    54 Mar 16 13:36 OneDrive - Code2care
drwxr-xr-x@   5 c2ctech  staff   160 Mar 24 14:09 IdeaProjects
drwxr-xr-x   25 c2ctech  staff   800 Apr  5 19:34 anaconda3
drwx------@   3 c2ctech  staff    96 Apr 13 21:51 Applications
drwxr-xr-x    5 c2ctech  staff   160 Apr 16 10:31 AI
-rw-r--r--    1 c2ctech  staff    29 Apr 18 19:33 vimrc
drwxr-xr-x@   3 c2ctech  staff    96 Apr 19 20:09 AndroidStudioProjects
drwx------+   7 c2ctech  staff   224 Apr 22 20:16 Documents
drwxr-xr-x@   3 c2ctech  staff    96 Apr 25 14:02 Postman
-rw-r--r--@   1 c2ctech  staff   433 May  3 17:33 gif-gen.py
drwx------+   6 c2ctech  staff   192 May  5 22:59 Pictures
drwx------@  93 c2ctech  staff  2976 May  6 00:46 Library
drwx------+ 120 c2ctech  staff  3840 May  6 17:15 Downloads
drwx------@  10 c2ctech  staff   320 May  7 15:42 Desktop
-rw-r--r--    1 c2ctech  staff     0 May  7 15:56 myfile.txt
ls command to order list by date

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

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