TechNeate.com iPhoneate.com QueComico.com MiamiGlobalRadio.com

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

by menorah / February 3, 2017 1:00 PM EST

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 sort the files by date instead, showing the most recently modified items at the top of the `ls` command output. 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 sort the output of the `ls` command by the date and time of last modification, but for better results, you'll probably want to use it with the `-l` longlist flag. Let's review some useful ways to sort `ls` output by date.

Sort 'ls' output by date

  • The -t flag will sort the output of the ls command by the last modification date and time:
  • Open Terminal, and navigate to the directory you want to sort by date using ls
  • Apply the following command syntax:
    ls - lt
  • Press "Enter" to view the directory contents listed with ls by date

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

Display "ls" sorted by date, readability, and other criteria

Another option for sorting the output of `ls` by modification date is to use `-lt`, but also include `-h` for readable file sizes, and `-a` to show all files prefixed with periods. This also makes it easy to remember the `-halt` flag, using it as follows:

ls -halt

Reverse the output order by date with "ls"

If you want to reverse the order so that the most recently modified items are at the bottom of the ls command output, you can add the -r flag:

ls -haltr

The output will be the same except that it is displayed in reverse order, with the oldest modification date and time at the top and the most recent at the bottom.