howtouselinux

3 Ways to Sort by size in Linux ls command

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 by size in Linux ls command:

  • ls -lS  #sort by size, largest first, and output in long format
  • ls -lhS #sort by size, largest first, and output in long and human-readable  format
  • ls -lrhS #sort by size, largest last, and output in long and 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.

  • If you want to sort the output by file size, you can use the -lS option. This will show you the files in a long list format and sort them by size.
  • If you want to see the human readable form of file sizes, you can use the -lhS option. This will show you the files in a long list format and sort them by human readable file size.
  • If you want to see the reverse order of file sizes, you can use the -lrhS option. This will show you the files in a long list format and sort them by reverse order of file size.

 

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).

ls command sort by size with -lS option in Linux

To sort by file size in Linux ls command, we can use the -lS option . 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.

  • -S Sort by size (largest file first) before sorting the operands in lexicographical order.
  • -l (The lowercase letter “ell”.) List files in the long format. It will give detailed information of files in columnar 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

The following is the definition of columns.

  1. 1st Column – Record type and access permission.
  2. 2nd Column – Hard links of file.
  3. 3rd Column – Owner of file
  4. 4th Column – Group of the owner
  5. 5th Column – Files Size (Bytes)
  6. 6th Column – Date and Time
  7. 7th Column – Directory and File Name

 

ls command sort by size with -lhS option in Linux

The best way to sort by size in Linux ls command is using ls command with -lhS option. Open the terminal and type ls -lhS command. The largest file will be listed on the first and the output is in long and human-readable 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 -lh)command will give you the data in terms of Mb, Gb, Tb, etc.

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

ls command sort by size with -lrhS option in Linux

-r Reverse the order of the sort. This 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 tocao staff 132M 1 18 12:44 Wireshark 3.6.1 Intel 64.dmg
-rw-r–r–@ 1 tocao staff 170M 11 4 15:34 pgadmin4-6.1.dmg
-rw-r–r–@ 1 tocao 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

FAQ about ls command in Linux

Does the ls command show hidden files on my computer in Linux?

Yes, the ls command will show hidden files on your computer. By default, Linux doesn’t show hidden files, but you can use the “-a” flag to show all files, including hidden ones.

How can I see more information about a file using the ls command in Linux?

If you want to see more information about a file, you can use the -l flag with the ls command. This will show you the file’s permissions, owner, group, size, and last modified date.

How do I show only directories when using the ls command in Linux?

If you want to see only directories when using the ls command, you can use the “-d” flag. For example, the following command will show only directories: ls -d

 How do I sort the output of the ls command by file size?

The ls command in Linux is used to list files and directories. The output of the ls command is sorted alphabetically by default, but it can be sorted by file size using the -l or -S option. The -l option will show you the long listing of files, while the -S option will show you only the file sizes. To sort the output of the ls command by file size, use the -l -s option followed by the -h option, which sorts files by human-readable file sizes (e.g. KB or MB).

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!

 

 

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Liquid Web Managed Word Press