How to Sort Files By Size in Linux? [Command Line]

How To Sort Files By Size In Linux

Hello folks, in this article, we will discuss how to sort files by size in Linux. Most of you are familiar with the ls command which is used to list files in the current working directory. But by using some parameters with the ls command, We can sort the files by name, size, modification time, etc along with the system files that are not shown by default. In this tutorial, We are using Ubuntu 20.04 LTS.

Sort Files By Size in Linux Terminal

To list all files in the current directory, Open a terminal window by pressing Ctrl+Alt+T and enter the following command,

ls
sort-file-by-size-1

In the above command, use -l parameter to list all files in a long list format and -a to show hidden files.

ls -la
sort-file-by-size-2
Screenshot From 2022 01 30 12 03 04

To list and sort all files by size in descending order(by default), use -S parameter or enter the following command,

ls -laS
sort-file-by-size-3

The files are now listed in descending order by file size. To show the size in human-readable format, use -h parameter,

ls -laSh
sort-file-by-size-4

To sort the files by size in ascending order, use -r parameter,

ls -laShr
sort-file-by-size-5

Conclusion

That’s all for now, we discussed how to sort all files by size in Linux in both the order i.e. ascending and descending. We can also sort files by name, date, time, etc using the ls command. Thank you for reading!