Often you may want to sort all directories in a particular directory by size in Bash.
You can use the following syntax to do so:
du -a -h --max-depth=1 | sort -hr
This particular example will display all directories in the current directory in order by size from largest to smallest.
Here is what the various commands do:
- du -a -h: Obtain disk usage ( du ) information for all items ( -a ) in current directory in human readable format ( -h )
- –max-depth=1: Limit results to directories only
- -sort -hr: Sort results in reverse order ( -r ) using human readable format ( -h )
The following example shows how to use this syntax in practice.
Example: How to Sort Directories by Size in Bash
Suppose that we would like to sort the directories in our current directory based on size from largest to smallest.
First, we can uses the ls command to view the directories in our current directory:

We can see that the current directory contains the following directories within it:
- east
- north
- west
We can use the following syntax to display each of these directories in order based on their size from largest to smallest:
du -a -h --max-depth=1 | sort -hr
The following screenshot shows how to use this syntax in practice:

The output displays the directories in order based on their size from largest to smallest:
- The east directory has a size of 10k.
- The north directory has a size of 3.0k.
- The west directory has a size of 2.0k.
Note: If you would like to display the directories from smallest to largest instead then simply leave out the last r in the sort command.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to List All Files in Folder by Size
Bash: How to Delete All Files in Directory
Bash: How to Delete All Empty Lines in File
