Table of Contents
Why Disk Usage Tools Matter
Disk space issues are one of the most common operational problems on Linux systems. Disk usage tools help you:
- See which filesystems are full or filling up.
- Identify which directories or files are taking the most space.
- Monitor disk usage trends (e.g. logs growing quickly).
- Plan clean-up, resizing, or archiving.
This chapter focuses on the main command‑line tools for inspecting disk usage:
df(filesystem usage)du(directory/file usage)lsand related helpers- High‑level utilities (
ncdu,pydf, GUI options) - Practical workflows for troubleshooting “disk full” issues
`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
dfKey columns (for most Linux distros):
Filesystem– device or pseudo-device (e.g./dev/sda1,tmpfs).1K-blocks/Size– total size of the filesystem in 1 KiB blocks.Used– how much space is used.Available– how much space non-root users can still use.Use%– percentage of space used.Mounted on– mount point (e.g./,/home,/var).
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:
- Data blocks are used up.
- Inodes are used up (too many files).
To see inode usage:
df -iColumns then show:
Inodes– total inodes.IUsed– used inodes.IFree– free inodes.IUse%– inode usage percentage.
A high IUse% with low data usage typically means you have huge numbers of tiny files.
Common `df` options
df -h– human-readable sizes.df -H– SI units (powers of 10; MB, GB instead of MiB, GiB).df -T– also show filesystem type (e.g.ext4,xfs,btrfs).df -x tmpfs– exclude specific filesystem types.df -t ext4– include only specific filesystem types.
Example:
df -h -x tmpfs -x devtmpfsThis 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 /pathCommonly, you’re interested in the total for a directory:
du -sh /pathOptions used here:
-s– summarize (only total for each argument; no recursion output).-h– human-readable.
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:
- Follows directory structure.
- Counts file sizes based on what’s allocated on disk.
- May not immediately reflect deleted but still-open files (see “Hidden disk usage” below).
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 | tailOr, more efficient (summaries for immediate subdirectories only):
du -h --max-depth=1 /var | sort -hKey option:
--max-depth=N– limit how deepdurecurses before printing summaries.
Common patterns:
# Largest subdirectories of root
du -h --max-depth=1 / | sort -h
# Largest subdirectories of /home
du -h --max-depth=1 /home | sort -hUsing `du` with file types and symlinks
Some useful options:
-a– show all files, not just directories.-x– stay on a single filesystem (don’t cross mount points).-L– follow symbolic links.
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.):
- Have a large logical size.
- Use less actually allocated disk space.
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 -lhShows:
-l– long listing format.-h– human-readable sizes.
To sort by size:
ls -lhS # largest first
ls -lhSr # smallest firstTo include hidden files:
ls -lahSThis 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 20Interactive 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:
- Debian/Ubuntu:
sudo apt install ncdu- Fedora:
sudo dnf install ncdu- Arch Linux:
sudo pacman -S ncduBasic usage
Run ncdu on a path:
ncdu /
ncdu /var
ncdu /home
ncdu:
- Scans the directory tree and computes usage.
- Displays an interactive, sorted list of directories/files by size.
- Lets you navigate with arrow keys and delete items (if permissions allow).
Common keys (shown at bottom in ncdu):
- Up/Down – move selection.
- Enter – dive into directory.
- Left/
Backspace– go up one level. d– delete selected file/directory.g– toggle between sorting modes.q– quit.
Use ncdu in read-only mode if you’re worried about accidental deletes:
ncdu -r /Typical use cases
- Quickly finding what fills
/var(e.g.logs,cache,lib/docker). - Exploring users’ home directories for large media or backup files.
- Analyzing large application directories (
/opt,/srv).
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:
pydf– a colorful, improveddf-like display.dfc– enhanceddfwith nicer formatting.dust/dua– modern, often fasterdureplacements (Rust-based, etc.).
Their interfaces differ, but they generally:
- Summarize space usage more visually.
- Offer filtering, color, or better sorting out-of-the-box.
Check your distro packages or use your package manager to search:
apt search dfc
dnf search dust
pacman -Ss duaGUI disk analyzers
On desktop systems, graphical tools can present space usage as charts:
- GNOME:
Baobab(Disk Usage Analyzer). - KDE:
Filelight. - Others:
qdirstat,k4dirstat, etc.
Typical usage:
- Start the tool from menu or terminal (
baobab,filelight, etc.). - Select filesystem or directory to scan.
- Inspect graphical representation (treemap, ring chart, etc.).
- 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:
- Opens a file.
- Writes a lot of data to it.
- The file is then deleted (e.g. rotated log), while still open.
Then:
dfstill shows used space (blocks are not freed).ducan’t see the file (no directory entry).- Space is effectively “hidden” until the process closes the file or exits.
To find such cases, use:
lsof | grep deletedOr filter:
lsof +L1Once you identify the process:
- Restart the service, or
- Kill the process (if appropriate).
After that, space is freed and df will reflect it.
Bind mounts and multiple references
If the same directory is:
- Mounted multiple times (e.g. bind mounts),
- Or visible through different paths,
du can double-count or miss usage depending on options:
- Use
-xto avoid crossing into other filesystems. - Carefully choose the starting directory.
CoW / snapshot filesystems
On CoW filesystems (e.g. btrfs, some LVM configurations):
- Snapshots and reflinks can share data.
dumight not reflect the true backend usage if data is shared.
Normally:
- Use the filesystem’s native tools (e.g.,
btrfs filesystem du, snapshot tools) for precise accounting. - Use generic
duwhen you just want to know which directories look large from a user’s perspective.
Practical Disk Usage Workflows
When `/` is nearly full
Scenario: df -h shows / at 95%+.
- Confirm:
df -h /
df -i /Check both space and inodes.
- Find top-level heavy directories:
sudo du -h -x --max-depth=1 / | sort -h- Drill down into the largest directory, e.g.
/var:
sudo du -h -x --max-depth=1 /var | sort -h- If available, use
ncdufor faster exploration:
sudo ncdu /
# or focus on a likely culprit
sudo ncdu /var- 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 20Then 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
- Use
dfto see filesystem-level space and inode usage. - Use
duto identify which directories are consuming space. - Use
ls,find, and sorting to pinpoint individual large files. - Use
ncdu(or similar) for fast, interactive disk usage analysis. - Be aware of hidden usage from deleted-but-open files, and special filesystem behaviors.
These tools together form the core toolkit for diagnosing and resolving disk space issues on Linux systems.