How to Sort Output of ‘ls’ Command By Last Modified Date and Time

One of the commonest things a Linux user will always do on the command line is listing the contents of a directory. As we may already know, ls and dir are the two commands available on Linux for listing directory content, with the former being more popular and in most cases, preferred by users.

When listing directory contents, the results can be sorted based on several criteria such as alphabetical order of filenames, modification time, access time, version and file size. Sorting using each of these file properties can be enabled by using a specific flag.

In this brief ls command guide, we will look at how to sort the output of ls command by last modification time (date and time).

Let us start by executing some basic ls commands.

Linux Basic ls Commands

1. Running ls command without appending any argument will list current working directory contents.

$ ls 
List Content of Working Directory
List Content of Working Directory

2. To list contents of any directory, for example /etc directory use:

$ ls /etc
List Contents of Directory
List Contents of Directory

3. A directory always contains a few hidden files (at least two), therefore, to show all files in a directory, use the -a or --all flag:

$ ls  -a
List Hidden Files in Directory
List Hidden Files in Directory

4. You can as well print detailed information about each file in the ls output, such as the file permissions, number of links, owner’s name and group owner, file size, time of last modification and the file/directory name.

This is activated by the -l option, which means a long listing format as in the next screenshot:

$ ls -l
Long List Directory Contents
Long List Directory Contents

Sort Files Based on Time and Date

5. To list files in a directory and sort them last modified date and time, make use of the -t option as in the command below:

$ ls -lt 
Sort ls Output by Date and Time
Sort ls Output by Date and Time

6. If you want a reverse sorting files based on date and time, you can use the -r option to work like so:

$ ls -ltr
Sort ls Output Reverse by Date and Time
Sort ls Output Reverse by Date and Time

We will end here for now, however, there is more usage information and options in the ls command, so make it a point to look through it or any other guides offering ls command tricks every Linux user should know or use sort command. Last but not least, you can reach us via the feedback section below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 thoughts on “How to Sort Output of ‘ls’ Command By Last Modified Date and Time”

  1. I need your help dearly.

    1. What is the option to output the version information of ls?
    2. What is the ls option that prints the author or a file?

    Reply
    • @Gwolo,

      Check these commands…

      $ ls --version - prints ls command version info
      $ ls -l filename  - prints owner, group and permission of file
      
      Reply
  2. Thanks a lot. I initially wanted to list files by date modified. But, you went even further and explained how to reverse the order. This seemed to make my task even easier. Thanks a lot.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.