Linux is known for its all-powerful terminal commands that let users do just about anything they could use a GUI by just hitting a few keys on their keyboard.
One such very basic command is the ‘ls’ command which lists out files inside the current working directory. By default, the ‘ls’ command lists files in alphabetical order, but what if you wanted to view them differently?
In this article, we’re looking at how you can sort the ‘ls’ command’s output by date.
Also read: Ubuntu no WiFi adapter found: 3 Fixes
Sorting files by date
As mentioned above, the ‘ls’ command prints out files in alphabetical order by default. However, users can print files sorted by dates in human-readable formats by adding a simple flag.
Sort output by date
If you’re looking to sort the output of the ‘ls’ command by the last date and time of modification and don’t care about the format of the date displayed, use the ‘-lt’ flag with the command.
As soon as you press enter, you’ll see that the files have been listed with the most recently modified files on top and the least modified at the bottom.
Sort output by human readable date
If you want to sort files according to the latest modification timestamp but want the time to be easier to read, add the ‘-h’ flag. We also include the ‘-a’ flag to show dot prefixes.
Press enter, and you’ll see a list of all the files with the most recently modified files on top and the least modified at the bottom and times in an easily readable format.
Reverse sorting order
If you want the oldest modified files to show up first and the most recently updated files to show up last, use the ‘-r’ flag.
Press enter, and you’ll have a file list in the desired format.
Also read: No route to host: 6 Fixes
Someone who writes/edits/shoots/hosts all things tech and when he’s not, streams himself racing virtual cars. You can reach out to Yadullah at [email protected], or follow him on Instagram or Twitter.