How to sort “ls” Output in Ascending or Descending in Linux?

The main objective of the “ls” command is to list down the content of the current directory in Linux. However, the output of the “ls” command does not provide detailed information such as file size, user name, group name, date and time, etc. But when we use the “-l” option with the ” ls ” command, it displays the content in the table format, including all the file /directory information.

This guide explains the possible ways to sort the “ls” output in ascending or descending order. The content of this guide is defined below:

Before starting with the guide, first, look at the “ls” command.

How Does the “ls” Command Work?

The “ls” command is beneficial for listing all the content in the present working directory. The working of the “ls” command depends on its syntax.

Syntax:

$ ls [OPTION]... [FILE]...

The “ls” is the command keyword that lists the details of the files/directories by following the provided options. These supported options can be obtained via the “ls –help” command.

$ ls --help

How to sort “ls” Output in Ascending Order?

The “-l” option of the “ls” command is useful for showing the list of all files and directories. By default, the “ls -l” command displays all the data available in the “Home” directory. Moreover, the combination of the “ltr” option displays the list in ascending order according to the File/directory “date” and “month”.

$ ls -ltr

How to sort “ls” Output in Descending Order?

The combination of the “-lt” option is beneficial to reverse the above list. It displays the “Home” directory’s last modified content in descending order:

$ ls -lt

How to sort “ls” Output by Last Modified Date & Time in Human Readable Format?

The combination of the “lth” option of “ls” commands is beneficial to get the files and directories size sorted by the last modified date and time in Human Readable format as shown below:

$ ls -lth

How to sort “ls” Output in Reverse Order?

The “lr” option of the “ls” command can be utilized to sort the current “Home” directory output in reverse order:

$ ls -lr

How to sort “ls” Output by File Size?

The “S” option of the “ls” command represents the files and directories size. This section uses the “-l” option for sorting the desired output by file sizes as shown in the screenshot:

$ ls -lS

How to sort “ls” Output By Directories?

The user can list down the directories available in the Home or a particular directory at the top of the list with the help of the below given “ls” command:

$ ls -l --group-directories-first

How to sort “ls” Output Recursively?

The “-R” option means recursive. The combination of “-lR” shows all the files/directories and subdirectories, including the content as shown in the below screenshot:

$ ls -lR

That’s all about the ls working on getting output in ascending/descending order.

Conclusion

In Linux, the “ls” output can be sorted in ascending or descending order by using the combination of “-ltr” and “lt” options.  The “ls” commands also contain many other options to sort the “ls” output according to the file size, last modified date, reverse order, and much more. This guide has illustrated all the possible ways to sort the “ls” output in ascending or descending order.