ls-ltr Command in Linux | Explained

One of the most common operations on Linux is listing directory content regularly. This task checks the directory size and removes unimportant files or subdirectories. Also, it sorts the content based on their sizes, creation or modification date, and much more. The above-defined functionality can be easily achieved via the Linux built-in “ls” and the “dir” command. These commands list the specified directory content along with its associated information.

Quick Outline

  1. What is the “ls” Command in Linux?
  2. What is the “ls -ltr” Command in Linux?
  3. How Does the “ls -ltr” Command Work in Linux?
  4. How to Use the “ls -ltr” Command in Linux?
  5. What Does “ls -l” Command Do in Linux?
  6. What Does the “ls -t” Command Do in Linux?
  7. What Does the “ls -r” Command Do in Linux?

Let’s first start with the basics of “ls” Command.

What is the “ls” Command in Linux?

As the name implies the “ls” command “lists” the content of the specified directory. It is the most frequently used Linux command that also allows the users to get the information of the targeted directory. 

This information includes directory permissions, owner name, group name, creation and modification date, and size. All of the specified information is achieved through the supported options of the “ls” command.

After getting its basics, now move on to its extended version “ls -ltr”.

What is the “ls -ltr” Command in Linux?

The “ls -ltr” contains the combination of three supported options “-l”, “-t”, and “-r” of the “ls” command. This command lists the content of the specified directory in a (-l)long list format in(-r)reverse order which is sorted by its last modification date and time(-t). Simply put, it reverses the output of the “ls -l” command that shows directory content based on its modification date.

Let us break down the combination of the “ltr” for a better understanding:

  • -l(long list): It displays the directory content in long list format prepending with its associated information permission, size, owner, group, date, and time as well as its size.
  • -t(time): It sorts the content of the directory based on its creation or modification date.
  • -r(reverse): It sorts the directory content in reverse order(alphabetically or numerically).

How Does the “ls -ltr” Command Work in Linux?

The “ls -ltr” command works on the given file or directory whose content needs to be sorted out in reverse order. This functionality can be achieved through its generalized syntax which is stated below:

ls -ltr [File|Directory]

In the above syntax the “File|Directory” can be single or multiple according to the user’s requirements.

How to Use the “ls -ltr” Command in Linux?

The “ls -ltr” command is used to quickly know which file or subdirectory of the specified directory was created or modified recently. Moreover, this command is also beneficial to getting about the files or subdirectories that take too much storage of the given directory. 

This section carries the practical implementation of all these use cases with the help of below-stated examples.

Example 1: Default Behaviour of “ls -ltr” Command

This example demonstrates the default behaviour of the “ls -ltr” command practically:

ls -ltr

Output

The output first displays the “total” size of the current working directory. Then, it lists(-l) the directory content in (-r)reverse order (alphabetically) based on the last modification date and time(-t):

Furthermore, the above-displayed list contains the following columns or fields:

  • First Column: It specifies the given file or subdirectory “permissions”(read, write, and execute). Moreover, the “d” character refers to the directory, and “(hyphen)” denotes that it is a file.
  • Second Column: It shows the “no of associations” links with that file or subdirectory.
  • Third Column: It tells the “owner” name of the given file or subdirectory that created it.
  • Fourth Column: It shows the “group” name of the given file or subdirectory associated. The group contains multiple users that have the same permissions for the file/directory.
  • Fifth Column: It represents the file/directory “size” in “KB(kilobytes)” by default.
  • Sixth Column: It shows the creation and modification date and time of the associated file/directory.
  • Seventh Column: It lists the “name” of all available files and subdirectories of the current working directory, but not hidden. To see hidden files use the “-a” flag along with the “ls” command.

Example 2: Use the “ls -ltr” Command to List Specific Directory Content 

The “ls -ltr” command can also be used to reverse the long list information of the specified directory that is sorted according to its modification date.

To do so, specify the exact path of the targeted directory after the “ls -ltr” command in this way:

ls -ltr /home/linux/Downloads

Output

It can be observed that the specified directory content has been listed reversely based on its modification date and time:

Example 3:  Use the “ls -ltr” Command to Filter Out Regular Files in Reverse Order According to Their Modification Date

This example uses the “ls -ltr” command along with the “grep” to filter out regular files of the current directory in reverse order based on their modification date. The regular files are the normal text, binary, or source files:

ls -ltr | grep ^-

In the above command:

  • |(Pipe): The pipe character combines the “ls -ltr” and the “grep” command.
  • grep: The “grep” command acts as a filter that searches for the pattern or string matching with the given one.
  • ^(caret): This metacharacter starts searching the line that is started with the “(hyphen)” character.

Output

The above output shows the regular files of the current working directory in reverse order according to their last modified date and time. 

What Does “ls -l” Command Do in Linux?

The “ls” command followed by the “-l(long list)” supported options lists the content of the given directory based on their last modification date and time. It does not show directory content in reverse order.

Here is its practical implementation:

ls -l

Output

It can be analysed that the execution of the “ls -l” command listed the content of the given directory according to the date and time at which they were last modified:

Note: For more details on the “ls -l” command read our detailed guide on What Does ls -l Command Do in Linux?

What Does the “ls -t” Command Do in Linux?

The “ls -t” command displays the name of the specified directory content based on their modification date. Furthermore, it does not provide the associated information of the given directory as the “ls -l” command:

ls -t

Output

Here is the practical demonstration of the “ls -t” command:

The output only contains the current directory files and subdirectory names sorted according to their last modification date and time.

For Long List information use the “ls -t” command along with the “-l” option like this:

ls -lt

Output

Now the output of the “ls -t” command is in long list format follows the descending order(numerically) as shown below:

What Does the “ls -r” Command Do in Linux?

The “-r” supported option of the “ls” command reverses the output arranged based on the last modification date and time:

ls -r

Output

The below output shows the practical implementation of the “ls -r” command:

Here, the output shows the current working directory in reverse order.

To show output in a Long List information format, use it with the “-l” option in this way:

ls -lr

Output

In this output, the current working directory content is displayed in reverse order along with their associated information.

That is all about the “ls -ltr” command in Linux.

Conclusion

In Linux, the “ls -ltr” command is the combination of three supported options “-l”, “-t”, and “-r” of the “ls” command. This command displays the specified directory files and folders in a (-l)long list format (-r)reversely that are sorted by their last modification date and time(-t). 

It shows the output of the “ls -l” command in ascending order(numerically) and descending order(alphabetically). Furthermore, the combination of these three supported options can also be split and use each flag separately along with the “ls” command.