Skip to content

How to sort Is command by date?

  • by
  • 3 min read

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.

ls -lt

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. 

ls -halt

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.

ls -haltr

Press enter, and you’ll have a file list in the desired format.

Also read: No route to host: 6 Fixes

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>
Exit mobile version