How to Check Disk Usage in Debian 12 Bookworm System

This article provides a step-by-step guide on how to check disk usage in Debian 12 Bookworm using various command-line tools and graphical utilities.

Efficient disk usage management is crucial for maintaining a healthy and high-performing Debian system. Over time, storage can fill up with logs, temporary files, and software packages, which can lead to degraded system performance or even system crashes if left unchecked. This article provides a comprehensive guide on how to check disk usage in Debian 12 Bookworm using various command-line tools and graphical utilities.

Why Monitor Disk Usage?

Monitoring disk usage in Debian 12 is important for several reasons:

  • Preventing Storage Overflow: Running out of disk space can cause application failures, system instability, and even data loss.
  • Optimizing Performance: A nearly full disk can slow down the system, affecting file operations and system responsiveness.
  • Ensuring Proper Resource Allocation: Monitoring helps identify which files or directories are consuming the most space, allowing you to optimize storage usage.

Command-Line Tools to Check Disk Usage

1. Using the df Command

The df (disk free) command provides a summary of available and used disk space on the system. To display disk usage in a human-readable format, use:

 df -h

This will output something similar to:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   20G   28G   42% /
tmpfs           2G     0    2G    0% /dev/shm
tmpfs           2G   9M    1.9G   1% /run

Each column represents:

  • Filesystem: The storage device or partition.
  • Size: The total capacity of the filesystem.
  • Used: The space currently used.
  • Avail: The available space.
  • Use%: The percentage of used space.
  • Mounted on: The mount point of the filesystem.

To display information about a specific partition, use:

df -h /home

2. Using the du Command

While df provides an overview of disk space usage, the du (disk usage) command helps to analyze space usage on a per-directory basis. To check the disk usage of a directory, use:

du -sh /path/to/directory

For example:

du -sh /var/log

This will return:

2.3G    /var/log

To see disk usage of all subdirectories in the current directory:

du -h --max-depth=1

This allows you to quickly identify which subdirectory is consuming the most space.

3. Using the ls Command

To find large files in a directory, use:

ls -lhS /path/to/directory

The -l option lists details, -h makes sizes human-readable, and -S sorts results by file size.

For example, to find the 10 largest files in /var/log:

ls -lhS /var/log | head -10

4. Using the ncdu Command

ncdu (NCurses Disk Usage) provides a user-friendly, interactive way to analyze disk usage. Install it with:

sudo apt install ncdu

Run ncdu in a specific directory:

ncdu /home

It will display an interactive interface where you can navigate and analyze storage usage efficiently.

5. Using find to Identify Large Files

To locate files larger than 1GB, use:

find / -type f -size +1G -exec ls -lh {} +

Replace 1G with 500M to find files larger than 500MB.

6. Using fdisk and lsblk to Check Partition Usage

To list partition details, use:

sudo fdisk -l

To display mounted partitions and disk usage, use:

lsblk -f

GUI Tools for Checking Disk Usage

If you prefer a graphical interface, Debian 12 offers several GUI tools.

1. GNOME Disks Utility

GNOME Disks is a user-friendly tool to monitor and manage disk usage. To install it:

sudo apt install gnome-disk-utility

Launch it by running:

gnome-disks

2. Baobab (Disk Usage Analyzer)

Baobab provides a graphical representation of disk usage.

Install it with:

sudo apt install baobab

Run it from the terminal:

baobab

Or search for “Disk Usage Analyzer” in the applications menu.

Automating Disk Usage Monitoring

To ensure disk usage does not exceed critical levels, set up periodic checks using cron.

Example: Alert When Disk Usage Exceeds 90%

Create a script:

echo "#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "Warning: Disk usage is at $USAGE%" | mail -s "Disk Usage Alert" user@example.com
fi" > /usr/local/bin/check_disk.sh

Make it executable:

chmod +x /usr/local/bin/check_disk.sh

Add a cron job to run it daily:

crontab -e

Add the following line:

0 6 * * * /usr/local/bin/check_disk.sh

This runs the script every day at 6 AM and sends an email if usage exceeds 90%.

Conclusion

Checking disk usage in Debian 12 Bookworm is essential for maintaining system stability and performance. By using command-line tools like df, du, ncdu, and find, or GUI utilities like GNOME Disks and Baobab, you can efficiently monitor and manage your storage. Automating disk monitoring with cron jobs further ensures proactive management, preventing storage-related issues before they become critical.

By regularly monitoring your disk usage, you can ensure that your Debian 12 system runs smoothly and efficiently.