How to check disk usage by folder on Linux

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 folders are consuming the most storage space.

In this guide, we’ll show you how to check disk usage by folder on Linux, through both command line and GUI methods.

In this tutorial you will learn:

  • How to check disk usage with du command examples
  • How to check disk usage with Disk Usage Analyzer GUI utility

Checking disk usage by folder on Linux

CategoryRequirements, Conventions or Software Version Used
SystemAny Linux distro
Softwaredu, Disk Usage Analyzer
OtherPrivileged 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

Check disk usage by folder 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.

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     .

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:

$ 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

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. You can also check our guide on listing directories by size for even more command line examples.

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.

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

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

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

Closing Thoughts

In this guide, we saw how to check hard disk usage by folder on Linux through command line examples and a GUI application. Both the GUI and the command line 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.

source: https://linuxconfig.org/how-to-check-disk-usage-by-folder-on-linux/

Tags: