Skip to main content
Tips

Sort du Command Output by Size

Learn to use the du command with sorted output based on size.

Sagar Sharma

The du command is used to find out the space used by the directories and files in Linux. This means you can find files and directories taking the most and least size.

But by default, the du command won't let you sort the output by size.

So how do you sort du command output by size? Simple!

By piping the output to the sort command. And in this tutorial, I will share several examples of how you sort the output by size.

How to sort du command output

As I mentioned earlier, the du command does not have a built-in functionality to sort output so you'll have to pipe it to the sort command.

In the simplest form, it will look like this:

du /path/to/directory | sort
use du with sort command

But as you can see, the output is not human-readable, and also, there's no control over how many items it will show you.

And there comes the concept of using multiple flags to tune the output.

Such as if you want to have the same output as above but in human-readable form, then you can use the -h flag as shown:

du -h /path/to/directoy | sort -rh
get human readable output while sorting output of the du command

The reason why I used the -r flag with the du command is to reverse the output of the du command and it will show output in descending order (from largest to smallest files).

And I'm about to show you what else you can do with the du command.

1. Find the top 10 largest files

Previously, I explained how you can sort files in descending order, but now, what we want is the 10 largest files.

For that purpose, you have to trim the output except the first 10 lines and for that purpose, I will pipe it to the head command:

du -h /path/to/directory | sort -rh | head -n 10
Find top 10 largest files in Linux using the du command

2. Specify how deep to look for files

For those who don't know, there are subdirectories inside directories and the chain will continue till the final sub-directory appears.

By default, the du command will initiate the recursive search meaning it will look till the last subdirectory appears and you may not want this behaviour.

To specify how deep you want to look for files, you can use the --max-depth flag.

For example, if I want to look for a maximum depth of 2, then I will be using --max-depth=2 and syntax would look like this:

du -h --max-depth=2 /path/to/directory | sort -rh
Specify how deep to search using the du command with sort

Alternatively, if you want to find out the 10 largest files with the --max-depth flag, then, you can refer to the following syntax:

du -h --max-depth=2 /path/to/directory | sort -rh | head -n 10
Look for max depth=2 and find the 10 largest files using the du command

3. Sort files based on filetype

This is an interesting one. How many times have you been looking for a file but you can't remember the filename? Multiple times (at least it happened to me).

But the one thing that is common is we know the file type. Such as you may have forgotten the name of the script but it will have the .sh extension is for sure.

The du command makes it more relevant by letting you sort the files.

To use the du command to soft files based on filetypes, all you have to do is append the filetype at the end of the path of the directory as shown here:

du -h /path/to/directory*.<File_extension> | sort -rh 

For example, if I want to sort all the .mkv files, then I will be using the following:

du -h ~/Downloads/*.mkv | sort -rh
sort files based on filetype using the sort command

4. Sort files within the specific filesize range

Sorting files within a specific size range is one of the best ways to sort files. Sadly, the sort command does not have any built-in functionality to do so.

Here's where the involvement of the find command comes in by letting you declare the minimum or the maximum threshold to initiate the search.

In this section, I will walk you through the following possibilities:

  • Find files less than the given size
  • Find files larger than the specified size
  • Find files within the given range (more than x and less than y)

So let's start with the first one.

Sort files that are less than the specific size

To sort files that are less than the given size, you'll have to combine the find command with the du and sort command as shown here:

find ~/Path/to/file -type f -size -<file_size> -exec du -h {} + | sort -rh

Here,

  • -type -f: Indicates you want to look for the files only.
  • size: To initiate a search based on the size parameter.
  • -<file_size>: Here the - symbol before the file size indicates that the file should be less than the specified size.
  • -exec du -h {}: Executes the du command enabled with a human-readable flag.
  • sort -rh: Uses sort command in reverse with a human-readable flag (-h).

Let's say I want to find files that are less than 1GB, then, I will be using the following:

find ~/THE_DU/ -type f -size -3G -exec du -h {} + | sort -rh
Sort files less than specific filesize

Sort files that are larger than the specified size

With a slight change in the above command, you can find the files that are larger than the specified size:

find ~/Path/to/file -type f -size +<file_size> -exec du -h {} + | sort -rh

As you can see, all I had to do was use + sign before the file size and it will sort the files that are larger than the given range.

For example, here, I sorted files that are larger than 3GB:

find ~/Downloads/ -type f -size +3G -exec du -h {} + | sort -rh

Sort files within the given range

As you can guess from the title, here, you have to create a range where you will specify the minimum and maximum range of the file.

To specify the minimum range, you use -size -<File_size> and for the maximum, you use -size +<File_size> making syntax look like this:

find ~/Path/to/file -type f -size +<file_size> -size -<file_size> -exec du -h {} + | sort -rh

For example, if I want to sort files that are between 3GB to 5GB, then, I will use the following:

find ~/Downloads/ -type f -size +3G -size -5G -exec du -h {} + | sort -rh
sort files within the given filesize range

Next: A modern alternative to the du command

If you are looking for a modern alternative to the du command or you just want to have a better version of the du command then you must try the duf command.

It is a combination of the du and df with modern colorful output and here's how you install and use the duf command in Linux:

How to Use the duf Command in Linux
The duf command is a modern utility that combines the features of the du and df commands in a pretty and structured way.

I hope I made sorting files using the du command made easy for you.

Sagar Sharma