When it comes to tidying up your hard drive on Linux, either to free up space or to become more organized, it's helpful to identify which directories are consuming the most storage space.

In this guide, we'll show you how to list all directories and sort them by their total size on Linux, through command line examples, a Bash script, and GUI methods.

In this tutorial you will learn:
  • How to list directories by size with du command examples
  • How to list directories by size with a Bash script
  • How to check directory sizes with Disk Usage Analyzer GUI utility
Listing the biggest directories on Linux
Listing the biggest directories on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software du, Disk Usage Analyzer
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

List directories by size via command line

The df and du command line utilities are the two best tools we have to measure disk consumption on Linux. For checking disk usage by folder, the du command is particularly useful.

When running du without any extra options, keep in mind that it will check the total disk usage of each subdirectory, individually. Depending on how deep your folder structure goes, this could be a massive amount of directories, and your terminal will be spammed with a lot of output.

In the following example, we run du on a directory full of Linux ISO files, but it's only one directory deep. We'll append the -h (human readable) option so it's easier to see what kind of space these directories are consuming.

$ du -h
11G     ./AlmaLinux
671M    ./Arch Linux
14G     ./CentOS
349M    ./Debian
1.9G    ./Fedora
415M    ./Gentoo
6.5G    ./Kali Linux
9.4G    ./Ubuntu
44G     .

We can see that the AlmaLinux folder is using 11 GB, and the Debian folder is only using 349 MB. The total of all these folders is 44 GB, as indicated in the last line.

Let's see what happens if we have a deeper folder structure.

$ du -h
671M    ./Arch Linux
6.5G    ./Debian-based/Kali Linux
9.4G    ./Debian-based/Ubuntu
17G     ./Debian-based
415M    ./Gentoo
11G     ./RHEL-based/AlmaLinux
14G     ./RHEL-based/CentOS
1.9G    ./RHEL-based/Fedora
27G     ./RHEL-based
44G     .

SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


As you can see, the "Debian-based" and "RHEL-based" directories have two and three subdirectories, respectively. This gives us a rather granular look at how much space each subdirectory is using. If you have a deep structure, we can use the --max-depth=N flag to tell du how many subdirectories deep it should traverse.

$ du -h --max-depth=1
671M    ./Arch Linux
17G     ./Debian-based
415M    ./Gentoo
27G     ./RHEL-based
44G     .

To sort these directories by size, making it easy to identify which ones are consuming the most space, we can pipe our du command to the sort utility. If you're using the -h option on du, make sure you also use it on sort.

$ du -h | sort -h
415M    ./Gentoo
671M    ./Arch Linux
1.9G    ./RHEL-based/Fedora
6.5G    ./Debian-based/Kali Linux
9.4G    ./Debian-based/Ubuntu
11G     ./RHEL-based/AlmaLinux
14G     ./RHEL-based/CentOS
17G     ./Debian-based
27G     ./RHEL-based
44G     .

Or, to limit the number of directories that are recursively traversed:

$ du -h --max-depth=1 | sort -h
415M    ./Gentoo
671M    ./Arch Linux
17G     ./Debian-based
27G     ./RHEL-based
44G     .

In these examples, we've been running du from our present working directory. Keep in mind that you can specify any directory with the command - you don't have to actually be in the directory you're checking.

$ du -h /home/linuxconfig

If you try to run du on your root directory to see storage space across the entire disk, keep in mind that you'll need to execute that command with root privileges and you should redirect standard error to /dev/null since you'll get a lot of "permission denied" spam in your output.

$ sudo du -h --max-depth=1 / | sort -h 2> /dev/null

List the top 10 biggest directories

Using the commands above, even small directories will be listed in the du command output. However, many times you may only be interested in the 10 biggest directories, or some number similar to that. In that case, we can pipe our output to the head command to only show the biggest directories. Note that we also use the tail command to avoid printing the size of the parent directory, which would be irrelevant in this case.

$ du -h --max-depth=1 2> /dev/null | sort -hr | tail -n +2 | head

Bash script for listing directories by size

The du and sort commands, along with the options we've gone over, should be enough to help you easily check disk usage by folder. Instead of remembering these various command options, and having to type them each time, we can make our lives a little easier by putting all of this into a Bash script.

The following Bash script will accept 2 arguments. First argument will be a directory name in which we will start our search and the second argument will by a number of directories the script should output.

#!/bin/bash 
 
if [ $# != 2 ]; then 
	echo "Incorrect number of arguments !" >&2 
	echo "USAGE: sortdirbysize [DIRECTORY] <first n directories>" 
fi 
du -h --max-depth=1 $1 2> /dev/null | sort -hr | tail -n +2 | head -$2

And here's an example of how you'd execute the script from command line:

$ ./sort-dir-by-size.sh /home/linux 15


Check disk usage by folder via GUI

Sometimes, it's easier to visualize disk usage if we use a GUI utility. One such application is called Disk Usage Analyzer, but it may not be installed by default on your Linux distro. Use the appropriate command below to install it with your system's package manager.

Sometimes, it's easier to visualize disk usage if we use a GUI utility. One such application is called Disk Usage Analyzer, but it may not be installed by default on your Linux distro. Use the appropriate command below to install it with your system's package manager.

To install Disk Usage Analyzer on Ubuntu, Debian, and Linux Mint:

$ sudo apt install baobab

To install Disk Usage Analyzer on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install baobab

To install Disk Usage Analyzer on Arch Linux and Manjaro:

$ sudo pacman -S baobab

After it's installed, search for and open the application.

Open Disk Usage Analyzer from the applications launcher
Open Disk Usage Analyzer from the applications launcher

When the program opens, it will ask if you want it to scan the home directory or an entire disk. You can also click the options menu (three stacked lines) for the ability to scan a particular folder.

Choose to scan the home folder, whole disk, or select a particular directory
Choose to scan the home folder, whole disk, or select a particular directory

Make your selection and the utility will begin scanning for files. Once it finishes scanning for content, it'll give you a full readout of how your hard disk space is being distributed to various directories on your system. There's also a graphical representation which you can move your mouse cursor over to get an even better idea. It lists directories by size, so you can quickly determine what's chewing up the most disk space.

Disk Usage Analyzer shows how storage space is being used in different directories
Disk Usage Analyzer shows how storage space is being used in different directories

Closing Thoughts

In this guide, we saw how to list directories and sort them by their total size on Linux. This involved various command line examples, centering mainly around the du utility, as well as a Bash script and GUI method. All methods are able to give us a quick summary of storage usage, or detailed breakdowns of how storage space is being used across various directories on our system.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.