howtouselinux

3 Ways to Sort files by Size in Linux

Table of Contents

If you’re a Linux user, you may have noticed that the ls command sorts files alphabetically by default. This can be helpful if you want to quickly find a specific file, but what if you want to see the largest files first?

In this article, we will show you how to sort the output of the ls command by file size.

3 ways to sort files by size in Linux:

  • ls -lS  #sort by file size, largest first, and output in long format
  • ls -lhS #sort by file size, largest first, output in long and human-readable  format
  • ls -lrhS #sort by file size, largest last, output in long and human-readable  format

 

Procedure to sort files by size in Linux

  • Open the terminal.
  • Type du -sh * | sort -hr and press Enter. This will show all the files in the current directory sorted by size in human-readable format.
  • Type ls -lSr and press Enter. This will show all the files in the current directory sorted by size in reverse order.
  • Type find . -type f -exec du -sh {} + | sort -hr and press Enter. This will show all the files in the current directory and its subdirectories sorted 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.

 

 

sort files by size with ls -lS command in Linux

we can use the -lS option to sort by file size in Linux ls command. These two options will cause ls command to sort files by their size, with the largest files appearing first and output is in long format.

Example:

% ls -lS
total 3151032
-rw-r–r–@ 1 test staff 502801251 8 29 2021 pycharm-community-2021.2.1.dmg
-rw-r–r–@ 1 test staff 177767487 11 4 15:34 pgadmin4-6.1.dmg
-rw-r–r–@ 1 test staff 138727831 1 18 12:44 Wireshark 3.6.1 Intel 64.dmg

 

sort files by size with ls -lrhS command in Linux

The -r option will display files and directories in reverse order. By default “ls” command sort by name that is files or directories name.

Example:

ls -lrhs
-rw-r–r–@ 1 howtouselinux staff 132M 1 18 12:44 Wireshark 3.6.1 Intel 64.dmg
-rw-r–r–@ 1 howtouselinux staff 170M 11 4 15:34 pgadmin4-6.1.dmg
-rw-r–r–@ 1 howtouselinux staff 480M 8 29 2021 pycharm-community-2021.2.1.dmg

  • The largest files are listed first with -S option.
  • Keep in mind that the -S only works for regular files (i.e., not directories).

ls command options in Linux

  • You can use the . symbol to represent the current directory. For example, if you want to list the contents of the current directory, you can just type: ls .
  • You can use the -a flag with the ls command to show hidden files. Hidden files are typically configuration files that start with a . (dot). For example, if you want to list all of the files in the current directory, including hidden files, you would type: ls -a
  •  You can use the -l flag with the ls command to show more information about each file, such as the permissions, owner, group, size, and modification time. For example, if you want to list all of the files in the current directory with more information, you would type: ls -l
  • You can use the -R flag with the ls command to recursively list the contents of all subdirectories. For example, if you want to list all of the files in the current directory and all subdirectories, you would type: ls -R
  • You can use multiple flags at the same time. For example, if you want to list all of the files in the current directory, including hidden files, with more information, you would type: ls -a -l
  • You can use the > symbol to redirect the output of a command to a file. For example, if you want to list all of the files in the current directory and save the output to a file called list.txt, you would type: ls > list.txt
  • You can use the >> symbol to append the output of a command to a file. For example, if you want to list all of the files in the current directory and append the output to a file called list.txt, you would type: ls >> list.txt
  • You can use the | (pipe) symbol to redirect the output of one command into another command. For example, if you want to list all of the files in the current directory and then sort them by size, you would type: ls -l | sort -n

 

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!

 

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.