Skip to Content

3 Ways to Sort files by Size in Linux

When dealing with a large number of files in a Linux system, organizing them based on their sizes can be immensely helpful.

In this post, we will explore three different ways to sort files by size in Linux, each providing its own advantages and flexibility. Whether you prefer a simple command-line approach or desire a more comprehensive file analysis, you’ll find the solution that best suits your needs.

Let’s dive in and discover the methods to effectively sort files by size in Linux.

In this article, we will show you how to sort the files by size with ls, du and find commands.

Procedure to sort files by size in Linux

  1. Open the terminal: Launch the terminal application.
  2. du -sh * | sort -hr: Lists files and directories in the current directory and sorts them by size in human-readable format.
  3. ls -lSr: Lists files in the current directory and sorts them by size in reverse order. Note that -S only works for regular files (not directories).
  4. find . -type f -exec du -sh {} + | sort -hr: Lists files in the current directory and its subdirectories, sorting them by size in human-readable format.

 

understanding ls command in Linux

The ls command is one of the most basic commands in Linux, and it is used to list the contents of a directory. By default, the ls command sorts files alphabetically, but you can also use it to sort files by size, by date, or by other attributes.

You can also use these options with other ls command options such as -a (to show all files) and -t (to sort by modification time).

sort files by size with ls -lhS command in Linux

 To sort files by size in Linux, you can use ls -lhS command.  Open the terminal and type ls -lhS command. The largest file under this directory will be listed first. The output is in a long and human-readable format.

  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar format.
  • -h option will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of byte. The (ls -h)command will give you the data in terms of Mb, Gb, Tb, etc.
  • -S Sort by size (largest file first) 

 

Example:

% ls -lhS
total 3151032
-rw-r--r--@ 1 test staff 480M 8 29 2021 pycharm-community-2021.2.1.dmg
-rw-r--r--@ 1 tets staff 170M 11 4 15:34 pgadmin4-6.1.dmg
-rw-r--r--@ 1 tets staff 132M 1 18 12:44 Wireshark 3.6.1 Intel 64.dmg

The following is the definition of columns.

 

 

This command is pretty useful when dealing with log files. For example, if we need to find the largest messages files under /var/log directory, we can use

ls -lhS /var/log/messages*

This command will list the files  in /var/log in a long format (-l) and sort them by size (-S). The largest message log file will appear at the top of the list.

[root@howtouselinux log]# ls -lSh messages*
-rw-------. 1 rsyslog root 3.9M Jun 3 10:24 messages
-rw-------. 1 rsyslog root 301K May 14 03:24 messages-20230514.gz
-rw-------. 1 rsyslog root 300K May 21 03:24 messages-20230521.gz
-rw-------. 1 rsyslog root 299K May 28 03:08 messages-20230528.gz
-rw-------. 1 rsyslog root 298K May 7 03:10 messages-20230507.gz

We can also use -r option to sort the files. The largest files will be listed on the last. Keep in mind that the -S only works for regular files ( not directories).

  • ls -lS: Sorts files by size, largest first, and outputs in long format.
  • ls -lhS: Sorts files by size, largest first, and outputs in long and human-readable format.
  • ls -lrhS: Sorts files by size, largest last, and outputs in long and human-readable format.

 

sort files by size with du command in Linux

We can also combine du and sort command to sort files by size in Linux. 

du -hs * | sort -h

This command will display the size of each file and directory in the current directory, and sort them in ascending order of size.

Here is a breakdown of the individual commands:

  • du -hs * : The du command is used to display the size of files and directories. The -h option makes the output human-readable, and the -s option displays only the total size of directory. * is a wildcard that matches all files and directories in the current directory.
  • |: The pipe symbol (|) is used to redirect the output of the preceding command (du -hs *) as input to the next command (sort -h).
  • sort -h : The sort command is used to sort the output of the du command. The sort command arranges lines of text in a specified order. The -h option tells sort to interpret the sizes as human-readable numbers.

 

example:

$ du -hs * | sort -h
0 a b
0 howtouselinux-d
0 tab
0 testfile1.img
0 testfile.img
4.0K howtouselinux
1.0G sparse_file.img

This command is helpful for quickly identifying the largest files or directories within a directory hierarchy and gaining insights into disk space utilization.

sort files by size with find command in Linux

find command in Linux is an easy way to quickly locate and manage large amounts of data. It can be used to search for files based on other criteria such as file permissions, modified date or type of file.

To sort files by size using the find command in Linux, you can use the following command:

$ find . -type f -exec du -sh {} + | sort -h
0 ./a b
0 ./.ssh/authorized_keys
0 ./tab
0 ./testfile1.img
0 ./testfile.img
4.0K ./.bash_logout
4.0K ./.bash_profile
4.0K ./.bashrc
4.0K ./howtouselinux
4.0K ./.lesshst
4.0K ./.ssh/known_hosts
12K ./.bash_history
1.0G ./sparse_file.img

 

  1. find . -type f: This command initiates a search in the current directory (.) and finds all files (-type f). It recursively searches through all subdirectories and returns a list of file paths.
  2. -exec du -sh {} +: This part of the command is executed for each file found by find. The du command calculates the disk usage (-h for human-readable format) of each file ({} represents the file path). The + at the end of the command is used to group multiple files together and process them efficiently.
  3. |: The vertical pipe symbol (|) is used to redirect the output of the previous command to the input of the next command.
  4. sort -h: This command takes the input received from the previous command and sorts it based on the human-readable file sizes (-h). The files are listed in ascending order, with the smallest sizes first.

 

Here is another way to get this.

find . -type f -printf "%s %p\n" | sort -n

This will search the current directory (represented by .) and all its subdirectories for regular files (-type f), and print the size and path of each file in a format that can be sorted by size. The -printf option is used to print the size (%s) and path (%p) of each file, separated by a space.

The output of the find command is then piped (|) to the sort command, which sorts the files by size (-n option). The -n option tells sort to interpret the sizes as numeric values, rather than text, so that it can sort them properly.

$ find . -type f -printf "%s %p\n" | sort -n
0 ./a b
0 ./.ssh/authorized_keys
0 ./tab
0 ./testfile1.img
17 ./.bash_logout
131 ./.bashrc
148 ./.lesshst
183 ./.bash_profile
354 ./howtouselinux
581 ./.ssh/known_hosts
11269 ./.bash_history
1073741824 ./sparse_file.img
2147483648 ./testfile.img

We can also use size option in find command to get the files which size is larger than the specified file size.

For example, to search for all files in the current directory that are larger than 100 megabytes, you would use the following command:

find . -size +100M

The find command can also be used to search for files that are smaller than a certain size, or that fall within a specific size range.

For example, to search for all files in the current directory that are smaller than 10 megabytes, you would use the following command:

find . -size -10M

To search for all files in the current directory that are between 10 and 100 megabytes in size, you would use the following command:

find . -size +10M -size -100M

Using find can be a huge time saver when you need to run bulk operations on large collections of data, and can be especially useful in managing servers with multiple users who have different levels of access.

We hope this article has helped you sort the output of the ls command by file size. If you have any questions or comments, please let us know in the comment section below. Thank you for reading!