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 files by size with ls, du and find commands.

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.

 

 

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 (i.e., not directories).

 

  • 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

 

sort files by size with du command in Linux

We can also use 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
  • 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

sort files by size with find command in Linux

Sorting files by size using the find command in Linux is an easy way to quickly locate and manage large amounts of data.

The basic syntax for this command is “find [directory] -size +[file-size]” which will search through each subdirectory of a given directory and output all files that are larger than the specified file size.

It can also be used to search for files based on other criteria such as permissions, modified date or type of file.

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.

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

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

The following command can give us the human readable formant size.

$ 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

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!

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn