The first line displays the entire size of the directory you are listing files in. When adding the -l option the output will display file permissions in the first column, the hard links, the owner, the group, the size in bytes, month, day and time and finally the filename.
If you want to sort this output according to file size, from bigger to smaller you need to add the -S (Sort) option.
As you can see the output lists the files and directories sorted by size, but in bytes which is not very human friendly (1 Byte is 0.000001 MB in decimal and 0.00000095367432 MB in binary).
To print the output in a human friendly way you only need to add the -h (human friendly) option:
As you can see in the output above, now file sizes are shown in GB, MB, KB and Bytes.
Yet you are only seeing regular files without hidden files, if you want to include hidden files in the output you need to add the option -a (all) as shown below:
As you can see hidden files (starting with a dot) are printed too now.
The following additional tips will help you to limit the output to a specific unit size different than bytes. The problem of this option is the output is never exact when the file is smaller or bigger than an exact number of that unit.
If you want to print in a specific size unit only instructing ls to display all files with the closest size in that unit only you can achieve it, for example, to print all files in MB you can add –block-size= options specifying MB with M as shown in the syntax and screenshot below:
As you can see now the size is in MB only, the bigger file is 115 MB, the second 69 MB, etc. Files with KB or Bytes size won’t be printed accurately, they will be shown as 1 MB which is the closest size can be printed if limited to MB.
The rest of the output remains exactly the same.
Using the same option (–block-size=) you can display the output in GB size instead of MB or bytes, the syntaxis is the same, replace the M for a G as in the example below:
You can also print the size in KB units by replacing the M or G for a K:
All the examples above will list files and directories sorted by size in the unit you want, with the clarified problem mentioned above the output wont be accurate for files which don’t match a exact unit size. Additionally, these examples didn’t include hidden files (which start with a .). To do so, you will need to add the option -a (all) which will display hidden files too, therefore, to print files sorted by size by bytes run:
As you can see now hidden files, starting with a . (dot) are printed, such as .xsession-errors, .ICEauthority, etc.
If you want to print files and directories sorted by size in MB including hidden files run:
To print or display all files and directories sorted by size shown in GB including hidden files run:
Similarly to previous commands, to print files and directories ordered by size shown in KB including hidden files run:
The command ls brings a lot of functionalities which help us to manage files and print information on them. Another example could be the use of ls to list files by date (with -lt options).
I hope you found this tutorial on How to list all files ordered by size in Linux useful. Keep following LinuxHint for more tips and updates on Linux and networking.