How to list all files ordered by size in Linux
Description
The ls command is one of the basic commands that any Linux user should know.
It is used to list information about files and directories within the file system.
The ls utility is a part of the GNU core utilities package which is installed on all Linux distributions.
In this article, we can see 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.
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.
ls -la
In the following command the -l flag means long listing and -a tells ls to list all files including (.) or hidden files.
To avoid showing the . and .. files, use the -A option instead of -a.
ls -la
OR
ls -la /var/www/html/
ls -laS
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/
ls -laSh
You can output the file sizes in human-readable format by adding the -h option as shown.
ls -laSh /var/www/html/
ls -laShr
And to sort in reverse order, add the -r flag as follows.
ls -laShr /var/www/html/
ls -laShR
Besides, you can list subdirectories recursively using the -R option.
ls -laShR /var/www/html/
We hope you’ve found this useful!