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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn active windows server 2008 + 2012 + 2016 + 2019 windows 10

Không có bản quyền Windows 10 sẽ bị ảnh hưởng gì? Bạn sẽ không thể tinh chỉnh các cài đặt...

Top 5 Tools for Taking and Editing Screenshots on Linux

Many times you feel the need to capture the screen or a part of it to show someone or to keep for yourself. While on Android,...

VIM Find and Replace

Vi/Vim is one of the legendary text editors out there that still finds its place at the arsenal of advanced and professional...