Linux ls Command List and Sort Files by Size

Linux ls Command List and Sort Files by Size

A directory in a Linux system can hold from a few files to hundreds and thousands of files.

You may need to sort the files by size, either in ascending or descending order. The reason for sorting files by size may vary. We may want to locate the largest to smallest files or vice versa.

You can easily sort files using the ls command.

In this tutorial, we’ll cover the various ways of sorting files by size using the ls command.

In our examples we’ll sort files in the /var/cache/apt/archives directory.


  • Access to a Linux machine.
  • Preferably acting as a non-root sudo user to ensure a secure environment

Listing the Files

To list files in Linux we commonly use the ls command. The ls command stands for listing and can be used without any parameter:


When we execute this command, the files in the current directory are listed.

word image 8

We can also use the -l parameter to generate the long listing of files and directories:

ls -l

The -l parameter provides several details.

word image 9

Sorting Files Based File Size

Up to this point, it is pretty simple. By default, the ls command sorts files alphabetically.

Sort Files in Descending Order (the -S parameter)

To make ls sort files by size, we can use the -S parameter, which sorts the files in descending order by default.

ls -lS

word image 10

Alternatively, you can get the same result by replacing -S with the --sort parameter, and you will get the same output.

To do this, we need to execute the following command:

ls -l --sort=size

Sort Files in Ascending Order (the -Sr parameter)

What if we need to sort the files by their size in ascending order – meaning the reverse than we did above.

We can do this by adding -r as a parameter with the -S parameter, which will basically just list the files in the reverse descending order, which is ascending.

ls -lSr

This time the output is generated in ascending order.

word image 11

As before, we can also use the --sort parameter. We’ll just add -r and files will be listed in reverse order.

ls -lr --sort=size

Sort Files by Size in a Human Readable Format

Rather than listing the files based on their byte size, we can also use the -h parameter to make the file size more human-readable.

It’s easier for humans to understand file size in megabytes or gigabytes rather than in long numeric format, such as 10472649.

Let’s try listing files, sorting them in ascending order (-Sr), and also use the -h parameter.

ls -lSrh

Now the files are sorted on their size in ascending order but in the human-readable format. In this command, we generate the listing of the files and directory, sorting on their file size in ascending order, and then converting the bytes to more human-readable formats like kilobytes.

In the output, instead of bytes size of something like 1984 or 1924, the file size is now listed as 1.9K.

word image 12


Well done. Hopefully, this tutorial helped you sort files based on their file size in Linux using the ls command. If we encountered any issues, please feel free to leave a comment or contact us, and we’ll get back to we as soon as we can.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like
Bash Until Loop
Read More

Bash Until Loop

Loop is a fundamental concept in computer programming languages. This concept can be used in bash scripts as…