How to sort the "ls" command by date in Mac -


Saturday, 4 February 2017

How to sort the "ls" command by date in Mac

The "ls" command lists all files and folders in a directory on the command line, but by default "ls" returns a list in alphabetical order. With a simple command prompt, you can have the files sorted by date instead, showing the most recently modified items at the top of the ls command results. This trick applies to the output of the ls command on Mac OS / Mac OS X, Linux, BSD, as well as Bash on Windows .

The -t flag will command the ls command to exit for the last modified date and time, but for best results, you'll probably want to apply it with the long-list indicator "-l". Let's look at some useful ways to sort out "ls" by date.

Sort output 'ls' by date

The -t flag will sort the output of the ls command by the last modification date and time:
Open Terminal, and scroll to the directory you want to sort by date with ls
Applies the following command syntax:
  Ls -lt 
Press "Enter" to see the contents of the directory listed with ls by date

The most recently modified articles will appear at the top of the command output, instead of displaying the returned list in alphabetical order.

Show "ls" sorted by date, readability and others

Another option to sort the output of ls by modification date is to use - lt but also include -h for readable read sizes, and - a to display all files prefixed by points. This also makes it easy to remember the - halt flag, using it as follows:
  Ls -halt

Invert the output order by date with "ls"

If you want to reverse the order so that the most recently modified elements are at the bottom of the output of the ls command, you can add the -r flag:
  Ls -haltr 
The output will be the same except it is shown in reverse order, with the oldest modification date and time at the top and most recently at the bottom.

No comments:

Post a Comment

Follow by Email