Are you wondering to sort ls command by date? The command ‘ls’ lists all folders or files in a directory at the command line. But the command returns a list in alphabetical order by default. After using a simple command flag, you have ls sort by date rather than, displaying the most recently changed items located at the top of the ls command results. The trick applies to the ls command output in Linux, Mac OS / Mac OS X, BSD, or Bash in Windows.
The -t flag can sort ls command output after the last modified time or date. But for the good results, you’ll really want to apply it with the -l long listing flag, and maybe some others as well. Come let’s review a few helpful methods to sort ls output by date.
Sort ‘ls’ Command Output by Date
The -t flag will categorize the ls command output by last date and time changed:
Step 1:
Head over to the Terminal if you can’t do so already (/Applications/Utilities/ in Mac OS) and move to the directory you want to sort by date with ls
Step 2:
Issue the following command syntax:
ls -lt
Step 3:
Press return to view the directory contents listed with ls by date
The most currently modified items will be displayed at the top of the command output, besides displaying the returned list in alphabetical order.
Display ls Sort by Date, Human Readable, All Files
My personal preference for classifying ls output by date modified is to use -lt but also adds -h for human readable sizes, and -a for displaying all dot prefixed files as well. Also, it makes it an easy to remember flag of -halt, used like so:
ls -halt
Reverse the order of ls Sort by Date Output
If you wish to reverse the order so that the recently modified items are under the ls command output, you can aslo add the -r flag as well like so:
ls -haltr
The output will be the same besides it has been displayed in reverse order. But with the oldest changed the date on top and recently modified time or date under it
Obviously, the technique applies to the Terminal or command line. But after sorting by date and variations of date changed. Or the last date opened can be amazingly essential for Finder as well. For instance, arranging the Mac Finder ‘All My Files’ by the last date opened is the best tip that can be applied similarly to any other folder displayed in Finder on the Mac to display the last time a file was modified or accessed.
Conclusion:
Here’s all about ‘Sort ls Command by Date’. Is this article helpful? Do you have any other tips or tricks for sorting directories by date? If yes then let us know in the comment section below!
Also Read: