Home Linux Commands How to List or Sort All Files by Size in Linux

How to List or Sort All Files by Size in Linux

The Linux ls command is a handy tool for listing files inside a directory. In the past, we have covered how to list and sort files by last modification time using the ls command.

In this article, we will go a step further and explore ways that you can list all the files in a specific directory and sort them by file size.

List or Sort All Files by Size in Linux

To list all files contained in a directory, launch your terminal and run the following command. When invoked without any arguments, the ls command simply lists the files and folders inside a directory.

$ ls

In the following command the -l flag means long listing and -a tells ls to list all files including hidden files – which are prefixed by a period (.). To avoid displaying the . and .. files, use the -A option instead of -a.

$ ls -la
List Files in Linux
List Files in Linux

List All Files Ordered by Size

To list all the files and sort them by size, use the -S flag. By default, it displays output in descending order (biggest to smallest in size).

$ ls -laS
List Files by Sizes
List Files by Sizes

You can display the output in a human-readable format by appending the -h option as shown.

$ ls -laSh
List Files Ordered by Sizes
List Files Ordered by Sizes

In addition, you can sort in reverse order (from the smallest to the largest) by adding the -r option as shown.

$ ls -laShr
List Files by Reverse Order Size
List Files by Reverse Order Size

List or Sort Files by Size According to a Specific File Type

You can list or sort out files by size by applying file type as a filter. For example, to list or sort zipped files by size, use the wildcard symbol as shown.

$ ls -lhS *.zip
List Specific File Type by Size
List Specific File Type by Size

Exclude Directories When Listing or Sorting Files by Size

To exclude directories in listing or sorting files by size, use the following syntax:

$ ls -Slh | grep -v '^d'
Exclude Directory When Listing Files
Exclude Directory When Listing Files

List or Sort All Files Using a Common Unit Size

In the above examples, the file sizes have been printed in different unit sizes, i.e Kilobytes (k) and Megabytes (M).

To print or display all file sizes in a specific unit, for example in Megabytes, add the --block-size= option specifying Megabytes as M as shown below.

$ ls -laS --block-size=M 
List Files by Megabytes
List Files by Megabytes

From the output, you can see the file sizes are now in MB only.

NOTE: For files less than the specified size (in this case MB), the size will be rounded off to the nearest MB. This implies files in Kilobytes and bytes will appear as 1MB. The same will hold true for any specified size.

You can also print the size in KB units by replacing M with k:

$ ls -laS --block-size=k 
List Files by Kilobytes
List Files by Kilobytes

This time around, the file sizes are accurately displayed in kilobytes since no file is less than a kilobyte, and hence no rounding off to the nearest kilobyte is done.

Conclusion

And that’s it for this lecture. In this article, we have demonstrated how to list or sort files by size in Linux. We hope you found this information insightful.

Winnie Ondara
My name is Winnie, a Linux enthusiast and passionate tech writer in Linux and DevOPs topics. I enjoy keeping abreast with the latest technologies in the Linux ecosystem and trying out new tools provided by the FOSS community.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.