Instructions to Use ls: Sort by Date Operation Guide

ls sort by date
ls sort by date

Whether your home setup or your work environment deals with so many directories, file management is an essential skill to have. Some Linux users think the ls command is only to list the files. In this article, let us see how you can use ls to sort by date.

This method works for all different use cases like log management, file editing, media organization, or plain file organization. Knowing how to sort files based on their last modified date or the creation date can save considerable time and effort. Today, let us look at the different methods by which you can sort files based on date in both ascending and descending order.

What does the ls Command do?

At Unixmen, we always start with the basics. This article is not an exception. To use the ls command to sort by date, first let us see what the ls command does. The ls command in Linux is used to list the files and directories either within the selected or at the specified directory. Usually the ls command is used to view files. With certain tweaks in place, you can use the ls command to sort the files in multiple ways, by the file size, name, and specifically in this article: by date.

How to Use ls to Sort Files by Date

Use the “-t” option along with the ls command to sort files by date. Executing this command lists the files in the order of their modification time. This sequence lists the files with the most recently modified files displayed first. Here is an example to get you started:

ls -lt

If you noticed, there are two options there: l and t. The “l” option gives a detailed listing. The “-t” option sorts the files by their last modification time. So by executing just one command, you can get two actions done.

If you would like to get the same action done but in reverse, i.e., the oldest files have to be shown first, execute the same command but with the “-r” option. The syntax for this command is:

ls -ltr

How to List Files by their Last Accessed Time

Sometimes, the last modified time is not the only date you would need. If you would like to see the files listed based on the time they were last accessed, even that is possible with ls command. Simply add the “-u” option to the ls command and the “-l” option we saw earlier to get the files sorted based on the time they were last accessed. Here is an example syntax:

ls -lu

Sometimes it becomes essential to know when a file was last read. To do this, you will need the detailed information along with sorting based on date. For this use case, you can execute the command with the “-lt” option:

ls -ltu

How to Use ls Command to Sort Files Based on File Creation Time

To sort the files in a directory based on their creation or inode change time, you can use the “-c” option along with the ls command. This command is useful to track changes made at the file metadata level. Execute this command to sort the file based on their creation time:

ls -lc

To get a more detailed listing, tweak the above command with the same “-t” option we saw earlier:

ls -ltc

Executing this command lists the files based on their inode change time. The list shows the most recently created files at the top. To reverse the sequence, i.e., the files created last to be shown at the top, use the “-r” option.

ls -ltrc

How to Get a Human Readable Output

When you use the “l” option for getting a detailed output, the file sizes are usually displayed in bytes. This is because machines communicate file sizes in terms of bytes. To make the output a little more reader friendly, combine the “-h” option for human readable file size. This option comes handy when you deal with large files.

ls -lth

This command sorts the files by their modification date and displays their sizes in KB, MB, or GB. This format makes it easier for people to understand.

Wrapping Up

File management is an essential skill when it comes to Linux system management. With ls, sort by date is an easy operation to do. This handy command can help you manage a large number of files. If you are looking for the most recent log file to view or if you want to delete the obsolete log file, this command is the saviour.

Related Link

Sysadmin tools: 11 ways to use the ls command in Linux | Enable Sysadmin (redhat.com)

Some More Articles that would be of Interest to You