Kahibaro
Discord Login Register

3.2.4 Disk usage tools

Why Disk Usage Tools Matter

Disk space issues are one of the most common operational problems on Linux systems. Disk usage tools help you:

This chapter focuses on the main command‑line tools for inspecting disk usage:

`df`: Filesystem Disk Space Usage

df reports space usage per mounted filesystem (not per directory). It’s usually the first tool you run when you get a “No space left on device” or similar error.

Basic usage

df

Key columns (for most Linux distros):

For human-readable output:

df -h

-h shows size in “human units” (KiB, MiB, GiB).

Focusing on a specific filesystem or path

You can pass a path; df shows the filesystem that path resides on:

df -h /var/log
df -h /home/user

This is especially useful when root (/) is on one partition and /home, /var, etc. are on others.

Showing inodes instead of blocks

Disk space can be “full” in two ways:

To see inode usage:

df -i

Columns then show:

A high IUse% with low data usage typically means you have huge numbers of tiny files.

Common `df` options

Example:

df -h -x tmpfs -x devtmpfs

This filters out virtual memory and device filesystems so you see only “real” disk-based filesystems.

`du`: Directory and File Disk Usage

While df tells you which filesystem is full, du tells you where inside it the space is used.

Basic usage

du /path

du recursively walks the directory and prints sizes for directories (and possibly files).

Output is in 1 KiB blocks by default. For human readable:

du -h /path

Commonly, you’re interested in the total for a directory:

du -sh /path

Options used here:

Examples:

du -sh /var
du -sh /home/*

The second command quickly shows usage per user home directory.

Understanding `du` vs actual usage

du reports space referenced by directory entries. It:

On some filesystems (e.g. thin-provisioned, CoW like btrfs), du may not exactly match df. Use it as a guide to “what’s big” rather than exact accounting.

Sorting by size

To find largest directories under a path:

du -h /var | sort -h | tail

Or, more efficient (summaries for immediate subdirectories only):

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

Key option:

Common patterns:

# Largest subdirectories of root
du -h --max-depth=1 / | sort -h
# Largest subdirectories of /home
du -h --max-depth=1 /home | sort -h

Using `du` with file types and symlinks

Some useful options:

Typical safe usage to avoid crossing mount points:

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

This avoids accidentally including e.g. /mnt/backup or remote filesystems when you’re only interested in root.

`du` and sparse files

Large sparse files (common with virtual machine images, database files, etc.):

By default, du reports allocated space, which is what you usually care about. If you need to check actual allocation vs apparent size:

du -h --apparent-size file.img
ls -lh file.img

--apparent-size reports logical size (what ls shows).

`ls` and File-Level Size Inspection

While du is for directory trees, ls quickly inspects file sizes in a single directory.

Human-readable sizes with `ls`

ls -lh

Shows:

To sort by size:

ls -lhS          # largest first
ls -lhSr         # smallest first

To include hidden files:

ls -lahS

This is useful when you suspect a single large file in a directory (e.g., huge log or backup file).

Combining `ls`, `du`, and `find`

Example workflow to find large files:

# Top 10 largest regular files under /var
find /var -type f -printf '%s %p\n' | sort -nr | head -n 10

Or using du:

# Largest files/directories under current directory
du -ah . | sort -h | tail -n 20

Interactive Disk Usage: `ncdu`

For many admins, ncdu becomes the go-to disk usage tool due to its interactive, navigable interface.

Installing `ncdu`

Depending on your distribution:

  sudo apt install ncdu
  sudo dnf install ncdu
  sudo pacman -S ncdu

Basic usage

Run ncdu on a path:

ncdu /
ncdu /var
ncdu /home

ncdu:

Common keys (shown at bottom in ncdu):

Use ncdu in read-only mode if you’re worried about accidental deletes:

ncdu -r /

Typical use cases

ncdu is especially helpful on systems where du is slow or cumbersome and you want an overview plus easy drill-down.

Other Disk Usage Helpers

Alternative CLI tools

Some distributions or admins use additional tools:

Their interfaces differ, but they generally:

Check your distro packages or use your package manager to search:

apt search dfc
dnf search dust
pacman -Ss dua

GUI disk analyzers

On desktop systems, graphical tools can present space usage as charts:

Typical usage:

  1. Start the tool from menu or terminal (baobab, filelight, etc.).
  2. Select filesystem or directory to scan.
  3. Inspect graphical representation (treemap, ring chart, etc.).
  4. Drill down and delete files as needed.

These tools are not usually installed on headless servers, but they can be useful when you have a desktop environment.

Hidden Disk Usage and Tricky Cases

Even when df and du don’t seem to agree, there’s usually a reason. Common tricky situations:

Deleted but still-open files

If a process:

  1. Opens a file.
  2. Writes a lot of data to it.
  3. The file is then deleted (e.g. rotated log), while still open.

Then:

To find such cases, use:

lsof | grep deleted

Or filter:

lsof +L1

Once you identify the process:

After that, space is freed and df will reflect it.

Bind mounts and multiple references

If the same directory is:

du can double-count or miss usage depending on options:

CoW / snapshot filesystems

On CoW filesystems (e.g. btrfs, some LVM configurations):

Normally:

Practical Disk Usage Workflows

When `/` is nearly full

Scenario: df -h shows / at 95%+.

  1. Confirm:
   df -h /
   df -i /

Check both space and inodes.

  1. Find top-level heavy directories:
   sudo du -h -x --max-depth=1 / | sort -h
  1. Drill down into the largest directory, e.g. /var:
   sudo du -h -x --max-depth=1 /var | sort -h
  1. If available, use ncdu for faster exploration:
   sudo ncdu /
   # or focus on a likely culprit
   sudo ncdu /var
  1. Investigate logs, caches, and temporary directories inside those paths.

Finding large log files quickly

# Largest files under /var/log
sudo find /var/log -type f -printf '%s %p\n' | sort -nr | head -n 20

Then inspect/rotate/truncate as appropriate (rotation details belong in logging/administration chapters, not here).

Checking per-user usage in `/home`

sudo du -sh /home/*

This lists total disk usage for each user home directory at a glance. Combine with ls -lh or ncdu inside a particular home directory to drill down.

Summary

These tools together form the core toolkit for diagnosing and resolving disk space issues on Linux systems.

Views: 134

Comments

Please login to add a comment.

Don't have an account? Register now!