Ubuntu df/du Commands: Checking Disk Space Usage

In Linux systems, disk space management is a crucial part of daily operations. Whether troubleshooting system failures, cleaning up redundant files, or planning storage, it is essential to first understand the disk usage. Ubuntu provides two commonly used tools: df and du, which help us view disk space usage from the perspectives of overall partitions and specific directories/files, respectively.

df Command: View Overall Disk Partition Usage

The df (Disk Free) command displays the space usage of entire disk partitions, including total capacity, used space, available space, and usage rate.

Basic Usage and Parameters

Running df directly shows information about all mounted partitions, but the default unit is bytes (unintuitive). It is recommended to use the -h parameter (human-readable format, automatically converting to KB/MB/GB):

df -h

Common parameters:
- -h: Display in human-readable units (e.g., 1K1K, 1.2G1.2G).
- -T: Show the filesystem type (e.g., ext4, xfs).
- -i: Show inode usage (Inodes are metadata-indexing nodes for files; a full inode table prevents creating new files).

Output Explanation

After executing df -h, an example output is:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       20G   15G  4.8G  76% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
/dev/sdb1       50G   10G  40G  20% /home
  • Filesystem: The mounted disk device or filesystem (e.g., /dev/sda2 is the second partition of the first disk).
  • Size: Total partition capacity.
  • Used: Used space.
  • Avail: Available space.
  • Use%: Disk usage rate (over 85% suggests cleanup).
  • Mounted on: Mount point (the directory where the partition is mounted, e.g., / is the root directory, /home is the user directory).

Key Notes

  • Mount Point: Linux associates disk partitions with the directory tree via “mounting”. For example, /home is mounted on /dev/sdb1, and user files are stored under /home/Username by default.
  • Special Partitions: tmpfs is an in-memory virtual filesystem with no physical disk space; its usage can be ignored.

du Command: View Detailed Directory/File Space Usage

The du (Disk Usage) command shows detailed disk usage of specified directories or files. Unlike df, which focuses on overall partitions, du focuses on specific content.

Basic Usage and Parameters

The basic syntax of du is du [options] [directory/file]. Common parameters:
- -h: Human-readable units (same as df -h).
- -s: Only show total sum (no subdirectory expansion).
- -a: Show all files and directories (including hidden files, which are hidden by default).
- -c: Show the total sum of all statistics at the last line.
- -sh: Combine -s (total sum) and -h (human-readable), suitable for quick directory size checks.

Common Examples

  1. View total size of a single directory:
    To check the total usage of /home:
   du -sh /home

Output: 2.5G /home (indicates /home occupies 2.5GB).

  1. View sizes of subdirectories in a directory:
    To list sizes of subdirectories in the current directory (without file details):
   du -h --max-depth=1

--max-depth=1 shows only first-level subdirectories (omitting this parameter recurses all levels).

  1. Show all files in a directory and sort by size:
    To view all files (including hidden files) in /tmp, sorted by size in descending order:
   du -ah /tmp | sort -hr | head -n 10
  • -a: Show all files (including hidden files).
  • sort -hr: Sort by size in descending order (-h enables human-readable sorting).
  • head -n 10: Show top 10 largest files.
  1. View size of a specific file:
    To check the size of bigfile.txt:
   du -h bigfile.txt

df vs du: Differences and Use Cases

Tool Scope Key Features Typical Use Case
df Entire disk partitions Shows total capacity, used/available space, with unified partition-level units Check if the system has overall disk shortages (e.g., root partition / usage >85%)
du Directories/files Displays detailed space usage of specified directories/files, with granularity to subdirectories Locate large files/directories (e.g., clean up cache, log files)

Summary: Disk Space Management Steps

  1. Check overall partitions: Use df -h to view partition usage and identify space-shortage partitions (e.g., / or /home).
  2. Locate large directories: Use du -sh /path/to/dir to check sizes of target directories, then du -h --max-depth=1 /path/to/dir to examine subdirectories.
  3. Clean redundant files: For large directories (e.g., Downloads, apt cache), delete unnecessary files or archive backups.

By combining df and du, you can efficiently manage disk space and avoid system anomalies caused by insufficient storage.

Xiaoye