Disk Management disk space

List Files Sorted by Size in a Directory Tree on Linux

Most file systems sort the files in alphabetical order of names. A user might, however, have some other preferences and wish to sort the files on the basis of their size. This might make the listing information more useful and productive. When it comes to file listing and sorting in Linux, we trust the ls command and would like to enable you to use it in a way that it perfectly customizes the output according to your preferences.

In this article, we will explain how you can list files, sorted by size, in a directory tree. This includes directory listing both in the graphical user interface and the command line.

We have performed the procedures and commands mentioned in this article on a Debian 10 Buster system. You can, however, replicate these steps on most Linux distros.

Sorting Files in a File Manager

The file manager that you are using has a default preference, mostly alphabetical, to sort files and folders in a directory. You can, however, set custom preferences so that it sorts your files based on their size. Nautilus is the default file manager for Linux Debian and Ubuntu. This is how you can sort the files, size-wise, on Nautilus in Debian.

Method1: Open the File Manager and then click on the downward arrow located in the top bar, as follows:

This will present a list of options on the basis of which you can sort the files and folders in a directory. Click on the Size option in order to sort the files on the basis of their decreasing size.

Method 2: You can also set the Preferences of your file manager in a way that it displays files and folders based on a given criterion. Click on the options button from the top bar as follows:

Then, click on the Preferences option that will open the Preferences view with the ‘Views’ tab open. Click on the List Columns tab that lets you set the order of information that appears in the list view of the File Manager.

Select the Size option and drag it to the top so that this criterion is the top preference to sort your files and folders.

Sorting Files in the Command Line

The ls command in Linux lets you list files and their information based on the criteria you specify in the form of various flags and their options. If no  criteria is specified, the ls command sorts all entries alphabetically. Here, we will use the ls command, with different flags, to make it list files and folders on the basis of their size.

You can open the Debian command line, the Terminal, by searching for it from the Application Launcher search as follows:

You can then use the -S option as follows to sort the files on the basis of size, largest first:

$ ls -S

Another option is to use the sort flag with the ls command using the following syntax:

$ ls  --sort=WORD

Where ‘WORD’ can be used to specify the following options:

  • none-instead of the -U flag
  • size- instead of the -S flag
  • time- instead of the -t flag
  • version- instead of the -v flag
  • extension- instead of the -X flag

Now that we have to sort our files on the basis of size, we will replace WORD with size as follows:

$ ls --sort=size

Here are a few options that will make the size sorting make more useful for you:

Print a long listing format

In order to print a list of size-sorted files in a lengthwise format, you can use the -l flag with the already described -S flag:

$ ls -Sl

You can see in the above image how the list is not displayed in a lengthwise format.

Reverse print a size-sorted list

If you want the ls command to print the lowest sized files first, you can use the -r flag along with the -S flag:

$ ls -Slr

The above command prints a lengthwise, revere size-sorted list of files and folders in a directory.

Print a more human-readable list of sorted files

The -h flag, when used with -S and -l, prints sizes like like 1K 234M 2G etc. which are more human readable. Here is how you can sort the files size-wise and make the list more human readable:

$ ls -Slh

Exclude directories when printing a size-sorted list

If you only want to list the files in a directory, and not the folders, sorted by size, you can make use of the grep command as follows:

$ ls -lS | grep -v '^d'

You can see that this command only lists files in a directory along with all the other information.

These were various ways through which you can not only sort the files on the basis of size, but also customize the listing some more by using some other commonly used flags. So whether you are on the Linux UI or the command line, you can now make the file listing much more informative.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.