How to Sort Files by Size in Linux Command Line

The ls command is used for displaying the contents of a directory. Use the option -l and you can list the files and directories along with their attributes.

Size is one of the attributes displayed by the ls -l command. Though you can see the sizes of the file, they are displayed alphabetically.

If you want to find the biggest or smallest files in the current working directory, you can sort the files by size.

The -S option of the ls command sorts the files by size.

However, you'll be more comfortable combining it with the options -h and -l:

ls -lhS

Why? I'll share it in detail with examples.

Sort files by size

There is a sort command in Linux. But it is used for sorting the content of files, not the files themselves.

You can display files in the current directory in the long listing format so that you can see their size:

abhishek@lhb:~/toto$ ls -l
total 404
-rw-rw-r-- 1 abhishek abhishek  41145 May 27 15:24 ads.txt
-rw-rw-r-- 1 abhishek abhishek  45443 May 24 09:03 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek  29983 May 27 16:07 bash.pdf
-rw-rw-r-- 1 abhishek abhishek 249773 May 26 14:56 cronjob-cheatsheet.png
-rw-rw-r-- 1 abhishek abhishek   4943 Jun  2 20:09 gnome-console-voiceover
drwxrwxr-x 4 abhishek abhishek   4096 Jun  2 22:09 letters
-rw-rw-r-- 1 abhishek abhishek  12721 May 29 12:29 members.2022-05-29.csv
-rw-rw-r-- 1 abhishek abhishek    143 May 30 12:06 routes.yaml
drwxrwxr-x 2 abhishek abhishek   4096 Jun  2 22:09 words

To sort files by size, use the option -S with the ls command. Mind it, it's capital S for sorting.

abhishek@lhb:~/toto$ ls -ls
total 404
-rw-rw-r-- 1 abhishek abhishek 249773 May 26 14:56 cronjob-cheatsheet.png
-rw-rw-r-- 1 abhishek abhishek  45443 May 24 09:03 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek  41145 May 27 15:24 ads.txt
-rw-rw-r-- 1 abhishek abhishek  29983 May 27 16:07 bash.pdf
-rw-rw-r-- 1 abhishek abhishek  12721 May 29 12:29 members.2022-05-29.csv
-rw-rw-r-- 1 abhishek abhishek   4943 Jun  2 20:09 gnome-console-voiceover
drwxrwxr-x 4 abhishek abhishek   4096 Jun  2 22:09 letters
drwxrwxr-x 2 abhishek abhishek   4096 Jun  2 22:09 words
-rw-rw-r-- 1 abhishek abhishek    143 May 30 12:06 routes.yaml

That's good but you can make it better by adding the -h option. This option makes the output of the ls command displays the file size in human readable formats.

This means now you can see files ordered by size in KB, MB, or GB.

abhishek@lhb:~/toto$ ls -lhS
total 404K
-rw-rw-r-- 1 abhishek abhishek 244K May 26 14:56 cronjob-cheatsheet.png
-rw-rw-r-- 1 abhishek abhishek  45K May 24 09:03 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek  41K May 27 15:24 ads.txt
-rw-rw-r-- 1 abhishek abhishek  30K May 27 16:07 bash.pdf
-rw-rw-r-- 1 abhishek abhishek  13K May 29 12:29 members.2022-05-29.csv
-rw-rw-r-- 1 abhishek abhishek 4.9K Jun  2 20:09 gnome-console-voiceover
drwxrwxr-x 4 abhishek abhishek 4.0K Jun  2 22:09 letters
drwxrwxr-x 2 abhishek abhishek 4.0K Jun  2 22:09 words
-rw-rw-r-- 1 abhishek abhishek  143 May 30 12:06 routes.yaml
💡
When two files are of the same size, they are sorted alphabetically.

List files by their size in reverse order

By default, it is sorted to put bigger files on the top. You can reverse the order and display the bigger files at the bottom. Put the reverse option -r with the rest.

ls -lhSr

This is particularly useful when you have numerous files (like log files) in a directory and you want to see the biggest ones. Instead of scrolling back, you can see the bigger files right on the screen.

abhishek@itsfoss:~/toto$ ls -lhSr
total 404K
-rw-rw-r-- 1 abhishek abhishek  143 May 30 12:06 routes.yaml
drwxrwxr-x 2 abhishek abhishek 4.0K Jun  2 22:09 words
drwxrwxr-x 4 abhishek abhishek 4.0K Jun  2 22:09 letters
-rw-rw-r-- 1 abhishek abhishek 4.9K Jun  2 20:09 gnome-console-voiceover
-rw-rw-r-- 1 abhishek abhishek  13K May 29 12:29 members.2022-05-29.csv
-rw-rw-r-- 1 abhishek abhishek  30K May 27 16:07 bash.pdf
-rw-rw-r-- 1 abhishek abhishek  41K May 27 15:24 ads.txt
-rw-rw-r-- 1 abhishek abhishek  45K May 24 09:03 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek 244K May 26 14:56 cronjob-cheatsheet.png
⚠️
All this is applicable to files only, not directories. Directories are always displayed as 4 KB (the block size) with the ls command. Their actual size is displayed with the du (disk usage) command.

Get the 10 biggest files in a directory

The best thing about the Linux command line is that you can combine various commands and get a different result.

Let's say you want to see some of the largest files in the current directory. Sort the files by size and then use the pipe redirection with the head command.

ls -lhS | head -11

In the example, I used 11 because the first line shows the total number of blocks used in the directory.

abhishek@itsfoss:~/tutu$ ls -lhS | head -11
total 216K
-rwxr-xr-x 1 abhishek abhishek  74K Jun  2 22:09 cpufetch
-rw-rw-r-- 1 abhishek abhishek  45K Jun  2 22:38 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek  41K Jun  2 22:38 ads.txt
-rwxr-xr-x 1 abhishek abhishek 4.9K Jun  2 22:09 README.md
-rw-rw-r-- 1 abhishek abhishek 4.9K Jun  2 22:38 gnome-console-voiceover
drwxr-xr-x 2 abhishek abhishek 4.0K Jun  2 22:09 doc
drwxr-xr-x 2 abhishek abhishek 4.0K Jun  2 22:09 pictures
drwxr-xr-x 5 abhishek abhishek 4.0K Jun  2 22:09 src
-rwxr-xr-x 1 abhishek abhishek 2.8K Jun  2 22:09 CONTRIBUTING.md
-rwxr-xr-x 1 abhishek abhishek 2.4K Jun  2 22:09 cpufetch.1

You can use the tail command with reversed sorting but that's too redundant, I think.

Everything is done in the current working directory. If you want to do it system-wide, you can start at / but then you need to use root privileges. But ls command is not suitable in such cases. Instead, use the find command to look for files bigger than a certain size.

Include hidden files while sorting files by size

If you add . (dot) at the beginning of a file or directory name, it is hidden from the normal listing views in the ls command. These are called hidden files.

Many configuration files are hidden from the normal views. While sorting for files by size, you should take these hidden files into consideration as well.

Option -a shows the hidden files including the special . and .. directories:

ls -lahS

Note the .member.csv file in the output:

abhishek@lhb:~/toto$ ls -lahS
total 428K
-rw-rw-r--  1 abhishek abhishek 244K May 26 14:56 cronjob-cheatsheet.png
-rw-rw-r--  1 abhishek abhishek  45K May 24 09:03 apt-get.pdf
-rw-rw-r--  1 abhishek abhishek  41K May 27 15:24 ads.txt
-rw-rw-r--  1 abhishek abhishek  30K May 27 16:07 bash.pdf
-rw-rw-r--  1 abhishek abhishek  13K Jun  3 09:41 .member.csv
-rw-rw-r--  1 abhishek abhishek  13K May 29 12:29 members.2022-05-29.csv
-rw-rw-r--  1 abhishek abhishek 4.9K Jun  2 20:09 gnome-console-voiceover
drwxrwxr-x  4 abhishek abhishek 4.0K Jun  3 09:41 .
drwxr-x--- 26 abhishek abhishek 4.0K Jun  3 10:19 ..
drwxrwxr-x  3 abhishek abhishek 4.0K Jun  3 10:10 letters
drwxrwxr-x  2 abhishek abhishek 4.0K Jun  2 22:09 words
-rw-rw-r--  1 abhishek abhishek  143 May 30 12:06 routes.yaml

Recursively sort files by size

So far you only dealt with files in the current directory. But what about the files in the sub directories?

If you are looking for the largest file in the current location, you should also consider looking for files in the sub directories.

You can use the option -R of the ls command to list files in the sub directories.

ls -lRS

However, this is not the correct approach because it doesn't sort all the files together, They are sorted in their respective directories.

Alternatively, you can use the du command and combine it to recursively sort files based on their size:

du -ah | sort -hr
📄
Many Linux users mistakenly use the -s (lowercase s) option to sort files by size. That's wrong. The lowercase s displays the block size for each entry. To sort, use the -S (uppercase S).

Conclusion

Now you know how to use the ls command to sort files based on their size. I hope you learned a few other things along the way.

If you are interested in learning more, I recommend these three topics:

Stay subscribed for more Linux tips and tutorials.