Skip to main content
Quick Tip

Sort ls Command by Date and Time

Looking for modified files? Learn how you can sort the output of the ls command by date and time.

Abhishek Prakash

By default, the ls command shows the result in alphabetical order.

If you want to sort the output by the modified time, you can use the option -t:

ls -lt

This way, the ls command lists the files that were recently modified first.

abhishek@itsfoss:~/test$ ls -lt
total 4712
-rw-r--r-- 1 abhishek abhishek   12817 Oct 20 09:56 sample.txt
drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir
-rw-rw-r-- 1 abhishek abhishek     106 Sep 27 20:39 redirects.json
-rw-rw-r-- 1 abhishek abhishek 4794657 Sep 27 20:36 export.json

As you can see in the above output, the content is sorted by date, not names.

Actually, these are sorted by time, not date. You'll notice that if you have multiple files that were modified the same day but at different times.

I'll show you all this in detail in this article.

Sort ls output by modified date and time

To sort the contents of a directory by time, use the -t option of the ls command.

ls -t

Combining it with the long listing option -l shows the timestamp as well.

ls -lt

Here's an example. See how the output of the ls command changes based on date and time.

Sort ls output by date and time

As you can see, the ls output is modified by time, not just date. Also, the time here is modified time (or mtime) by default. I hope you are familiar with the file timestamps in Linux. If not, this article will help you out.

File Timestamps in Linux: atime, mtime, ctime Explained
Let’s see what are the various kinds of file timestamps in Linux, how to see the timestamps for a file and how to change the timestamps.

What if two files have the same modified time?

The files with exact same modified time will be sorted alphabetically but they will be grouped together.

Let me show this with an example. This is the ls output sorted by time.

abhishek@itsfoss:~/test$ ls -lt
total 4712
-rw-rw-r-- 1 abhishek abhishek     106 Oct 20 11:54 redirects.json
-rw-rw-r-- 1 abhishek abhishek 4794657 Oct 20 11:37 export.json
-rw-r--r-- 1 abhishek abhishek   12817 Oct 20 09:56 sample.txt
drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir
-rw-rw-r-- 1 abhishek abhishek     311 Sep 22 12:19 hard_link

Let me use the touch command to change the modify time of two files. The two files will have the same modified time now. You can use the stat command to verify it.

touch export.json redirects.json

I'll also change the modified time of another file to show the grouping of files properly.

touch hard_link

Now let me sort the ls output based on time:

abhishek@itsfoss:~/test$ ls -lt
total 4712
-rw-rw-r-- 1 abhishek abhishek     311 Oct 20 11:58 hard_link
-rw-rw-r-- 1 abhishek abhishek 4794657 Oct 20 11:55 export.json
-rw-rw-r-- 1 abhishek abhishek     106 Oct 20 11:55 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Oct 20 09:56 sample.txt
drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir

As you can see, the hard_link file was the most recent and hence displayed at the top.

But both redirects.json and export.json have the same mtime. So they are grouped together but displayed in alphabetical order with export.json before redirects.json.

Interesting, isn't it?

Reverse sort the files by time

When you have a huge list of files in a directory and you want to see the recently modified files, you should sort by time or date but in reverse order.

This way, the recently modified files are displayed at the bottom. What's the benefit? You don't have scroll up the screen to see the recently modified files. It's a tiny thing but saves a little of your time and frustration when you have 10s or 100s of files.

To reverse the order, use the option -r. Combine it with -t and it will sort ls output by time but in the reverse order.

ls -lrt
Reverse sort by time with ls command

The ls -lrt is one of the most popular ls command examples. You can also use the find command to display files modified in a given timeframe.

Find Files Modified in Last N Minutes in Linux
Finding recently modified files is a helpful parameter when troubleshooting your code or server. Learn how to find modified files in Linux command line.

Sort ls output by access time

By default, the time in ls command is mtime (modified time). This is the time when the file was last modified.

There is also atime and ctime. The ctime is hardly used but the atime is represents the time when the file was last accessed.

For example, if you read the contents of a file with cat or less command, its atime should be changed while its mtime and ctime remains the same. This behavior is controlled by operating systems and some distros have noatime set for better performance.

To sort the ls command output by access time, you can use:

ls -lt --time=atime

The --time=atime tells -t to use access time.

Let me show this with an example. I use the touch command to modify only the access time.

touch -a sample.txt

By default, sample.txt will be down the line as it's modified time is older than some.

abhishek@itsfoss:~/test$ ls -lt
total 4712
-rw-rw-r-- 1 abhishek abhishek     311 Oct 20 11:58 hard_link
-rw-rw-r-- 1 abhishek abhishek 4794657 Oct 20 11:55 export.json
-rw-rw-r-- 1 abhishek abhishek     106 Oct 20 11:55 redirects.json
-rw-r--r-- 1 abhishek abhishek   12817 Oct 20 09:56 sample.txt
drwxrwxr-x 2 abhishek abhishek    4096 Oct 17 12:33 my_dir

However, when I explicitly tell it to use atime, the sample.txt file comes on the top of the result.

abhishek@itsfoss:~/test$ ls -lt --time=atime
total 4712
-rw-r--r-- 1 abhishek abhishek   12817 Oct 20 13:32 sample.txt
-rw-rw-r-- 1 abhishek abhishek     311 Oct 20 11:58 hard_link
-rw-rw-r-- 1 abhishek abhishek 4794657 Oct 20 11:55 export.json
-rw-rw-r-- 1 abhishek abhishek     106 Oct 20 11:55 redirects.json
drwxrwxr-x 2 abhishek abhishek    4096 Oct 20 10:02 my_dir

Do notice that the timestamp displayed now is also the atime.

Sorting ls command output by atime

Conclusion

I have shared a few real examples to show how you can sort the ls command output based on date and time. The information about the atime is not known to many but now you know about it too.

Questions? Suggestions? Feel free to leave a comment.

Abhishek Prakash