How to Use the `du` Command to Check Disk Usage on Debian 12 (Bookworm)

This article provides a step-by-step guide on how to use the du command to check disk usage on a Debian 12 (Bookworm) system.

Efficient disk space management is crucial for maintaining a healthy and responsive Linux system. On Debian 12 “Bookworm”, one of the most reliable and straightforward tools for monitoring disk usage is the du command. Short for “disk usage,” du allows system administrators and regular users alike to analyze how disk space is being consumed across files and directories.

In this article, we’ll explore how to use the du command on a Debian 12 Bookworm system. We’ll break down basic usage, explore useful options, and even look at combining du with other commands like sort and grep for advanced use cases.


Why Monitor Disk Usage?

Before diving into the technical details, let’s understand why monitoring disk usage is important:

  • Prevent system crashes: Running out of disk space can halt essential services or make the system unbootable.
  • Identify bloated directories: It helps pinpoint what’s consuming unnecessary space.
  • Routine maintenance: Regular checks are part of best practices for system administration.
  • Debug performance issues: Slow systems often stem from full or nearly full disks.

The du command, built into the GNU core utilities, is available by default on almost all Linux distributions, including Debian.


Getting Started with du

The du command does not require any installation, as it is already included in Debian’s default base system. You can confirm its presence using the which command:

which du

This will typically return /usr/bin/du.

To see the version of du:

du --version

On Debian 12, this should return something like:

du (GNU coreutils) 9.1

Basic Syntax of du

The general syntax of du is:

du [OPTIONS] [FILE or DIRECTORY]

If you run du without any options:

du

It will display the size of each subdirectory in the current directory, and end with the total size.

Example

$ du
4 ./Documents
8 ./Downloads
12 .

Here:

  • 4 and 8 are in kilobytes by default.
  • . refers to the current directory.

Commonly Used du Options

Let’s go over some of the most practical and commonly used options that make du more user-friendly.

1. -h (Human-readable)

Displays sizes in KB, MB, or GB for easier interpretation.

du -h

Output:

4.0K ./Documents
8.0K ./Downloads
12K .

2. -s (Summarize)

Shows only the total size of a directory, not each individual subdirectory.

du -sh /var

Output:

1.2G /var

3. -a (All files)

Includes files in the output (by default du lists only directories).

du -ah /home/username

This is useful if you want a more granular look at what’s using space.

4. --max-depth=N

Controls how deep into the directory tree du goes. Replace N with a number.

du -h --max-depth=1 /var

Output:

1.1G /var/log
56M     /var/cache
1.2G /var

5. --exclude=PATTERN

Skips files or directories that match the given pattern.

du -h --exclude="*.log" /var

This is useful when you want to skip logs, backups, or temporary files.


Analyzing Disk Usage with du + sort

du can be piped into sort to identify the largest files or directories. Here’s a handy one-liner:

du -ah /home | sort -rh | head -n 10

Explanation:

  • -a: Includes all files.
  • -h: Human-readable.
  • sort -rh: Sorts the output in reverse order, using human-readable sizes.
  • head -n 10: Shows the top 10 largest files or directories.

This is an excellent way to identify large files consuming space.


Disk Usage of a Specific File

Although du is designed primarily for directories, it works on individual files too:

du -h /home/username/Downloads/movie.mp4

Output:

700M /home/username/Downloads/movie.mp4

If you want just the file size without the path:

du -sh /home/username/Downloads/movie.mp4 | cut -f1

Combining du with find

To find files larger than a certain size and measure their disk usage:

find /home -type f -size +100M -exec du -h {} \;

This will show the disk usage of every file over 100MB.


Real-World Examples on Debian 12 Bookworm

Checking System Logs

System logs can fill up disk space quickly.

du -sh /var/log

You can go deeper:

du -h --max-depth=2 /var/log

Home Directory Analysis

To check what’s taking up space in your home directory:

du -h --max-depth=1 ~/

Script to Monitor Disk Usage

If you’re an administrator and want to automate disk usage reporting, here’s a simple bash script:

#!/bin/bash

REPORT="/tmp/disk_usage_report.txt"
DIRECTORY="/home"

echo "Disk Usage Report - $(date)" > $REPORT
du -h --max-depth=1 $DIRECTORY | sort -rh >> $REPORT

echo "Top 10 largest files:" >> $REPORT
du -ah $DIRECTORY | sort -rh | head -n 10 >> $REPORT

cat $REPORT

Save this as disk_report.sh, give it execute permission with chmod +x disk_report.sh, and run it whenever you need a report.


Troubleshooting and Tips

  • Permissions: If du shows “Permission denied” errors, use sudo.
sudo du -sh /root
  • Exclude mounted drives: Use -x to stay within the same filesystem.
du -hx / | sort -rh | head -n 10
  • Performance: For very large filesystems, du can be slow. Use ncdu (NCurses Disk Usage) for interactive analysis. Install it using:
sudo apt install ncdu

Then run:

ncdu /

Conclusion

The du command is a powerful and versatile utility that every Debian user should have in their toolbox. Whether you’re troubleshooting a full disk, cleaning up old files, or maintaining a clean system, du provides the essential data needed to make informed decisions.

On Debian 12 Bookworm, this command works flawlessly out of the box and integrates well with other Unix tools like sort, find, and grep. With just a few command-line invocations, you can take full control over how disk space is allocated and consumed on your system.

Remember, good disk hygiene isn’t just about reclaiming space—it’s also about ensuring system reliability and performance over time.