How to Sort Results From ls Command by Modification Date?

The “ls” built-in command line tool lists down the content of the files and directories except for hidden files. Its output only shows the name of the directory/file/subdirectories present in the current working directory. The wide range of ls command’s options allows you to get the output in the desired format.

This today’s post explains the working of the “ls” command to sort results by modification date.

How to Sort Results From ls Command by Modification Date?

This section explains the usage of the “ls” command to sort its results by modification date with the help of practical examples.

Example 1: Sort Output by Modification Date

The user “itslinuxfoss” is in the current working directory “home”.

Execute the “ls” command with the “-l(list)” and “-t(sort by date&time)” supported arguments to sort the “home” directory content according to modification date:

$ ls -lt

In the above command:

  • The “-l” flag displays the associated parameters of file/directory, i.e., permissions, number of subdirectories, user-name, group, size in kb, date and time.
  • The “-t” argument sorts the files and directories based on the last modified date.

The last and second last “date & time” column of the “ls” command shows that output is sorted by modification date.

Example 2: Sort Output of Particular Directory by Modification Date

The user can also arrange the content of the particular directory by the last modified date. Type the absolute path of the directory, i.e., “/home/itslinuxfoss/Downloads” with the “ls -lt” command:

$ ls -lt /home/itslinuxfoss/Downloads

The above command has sorted “Downloads” directory output by last modified date.

Example 3: Sort Output by Modification Date (Reverse Order)

The “ls” command can also sort the “ls -lt” output arranged according to the last modified “date&time” in reverse order using the “-r (reverse)” flag:

$ ls -ltr

At this time, the output has been reversed and the recent modified file/directory has been shifted in last.

Note: To sort the output of the “ls” command by file size, directories, and ascending/descending order read our detailed article using this link  “sort “ls” Output in Ascending/Descending in Linux

Conclusion

The “ls” offers the “-t” flag to sort its output as per the modification date. It is used with the “-l(list)” flag that displays the “date&time” of the file/directory. The output sorted by modified date can also be displayed in reverse order using the “-r(reverse)” flag.

This post has covered all possible aspects of sorting results from the “ls” command by modification date.