How to List All Files Ordered by Size in Linux
In one of our several articles about listing files using the popular ls command, we covered how to list and sort files by last modification time (date and time) in Linux. In this short handy article, we will present a number of useful ls command options to list all of the files in a certain directory and sort them by file size in Linux.
Recommended Read: How to Find Out Top Directories and Files (Disk Space) in Linux
To list all files in a directory, open a terminal window and run the following command. Note that when
ls invoked without any arguments, it will list the files in the current working directory.
In the following command the
-l flag means long listing and
ls to list all files including
(.) or hidden files. To avoid showing the
.. files, use the
-A option instead of
$ ls -la OR $ ls -la /var/www/html/admin_portal/
To list all files and sort them by size, use the
-S option. By default, it displays output in descending order (biggest to smallest in size).
$ ls -laS /var/www/html/admin_portal/
You can output the file sizes in human-readable format by adding the
-h option as shown.
$ ls -laSh /var/www/html/admin_portal/
And to sort in reverse order, add the
-r flag as follows.
$ ls -laShr /var/www/html/admin_portal/
Besides, you can list subdirectories recursively using the
$ ls -laShR /var/www/html/admin_portal/
You will also find the following related articles useful:
- How to Find Recent or Today’s Modified Files in Linux
- Linux ‘tree Command’ Usage Examples for Beginners
- 10 Practical Examples Using Wildcards to Match Filenames in Linux
- Ways to Use ‘find’ Command to Search Directories More Efficiently
If you any other way to list the files ordered by sizes in Linux, do share with us or do you have questions or thoughts to share about this guide? If yes, reach us via the feedback form below.