du Disk Usage

du cheatsheet — check directory sizes, find large files. du -sh *, du -h --max-depth=1, du -sh /var | sort -h. Find what's eating your disk space.

5 min read

What it is

du estimates file space usage, typically used to find out how much disk space specific files or directories are consuming.

Installation

Linux: du is a core utility and is pre-installed on most Linux distributions. If missing, install via your package manager:

sudo apt update && sudo apt install coreutils # Debian/Ubuntu
sudo yum install coreutils # CentOS/RHEL
sudo dnf install coreutils # Fedora

Mac: du is a core utility and is pre-installed on macOS.

Windows: du is not a native Windows command. You can install it via:

  • Git Bash: If you have Git for Windows installed, du is available within the Git Bash terminal.
  • WSL (Windows Subsystem for Linux): Install a Linux distribution from the Microsoft Store and use du within that environment.

Commands / Usage

Basic Usage

  • Check current directory’s total size:

    du
    

    Shows disk usage for all subdirectories in the current directory, in blocks (usually 1KB).

  • Check a specific directory’s total size:

    du /var/log
    

    Shows disk usage for /var/log and its subdirectories.

  • Check size in human-readable format:

    du -h
    

    Shows disk usage with sizes in KB, MB, GB (e.g., 1.5G, 250M).

  • Check a specific directory’s size in human-readable format:

    du -h /home/user/documents
    

    Shows disk usage for /home/user/documents and its subdirectories in human-readable format.

  • Display only the total size of a directory (summary):

    du -sh /home/user/projects
    

    Shows the grand total size of /home/user/projects in human-readable format.

Controlling Depth and Output

  • Show usage for the current directory and its immediate subdirectories only:

    du -h --max-depth=1
    

    Shows disk usage for the current directory and only one level of subdirectories.

  • Show usage for a specific directory and its immediate subdirectories only:

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

    Shows disk usage for /etc and its immediate subdirectories.

  • Show usage for all files and directories, including hidden ones:

    du -a
    

    Lists disk usage for all files and directories, not just directories.

  • Sort output by size:

    du -h | sort -rh
    

    Lists disk usage for all subdirectories, sorted from largest to smallest.

  • Find the largest directories (top 5):

    du -h --max-depth=1 | sort -rh | head -n 5
    

    Finds the 5 largest directories (including the current directory) in the current location.

Appending Information

  • Append human-readable size to each line:

    du -h -a
    

    Same as du -a, but with human-readable sizes.

  • Append apparent size (actual file size) instead of disk usage:

    du -h --apparent-size
    

    Shows the actual size of files, which can be smaller than disk usage due to sparse files or block allocation.

  • Append apparent size and sort by it:

    du -h --apparent-size -a | sort -rh
    

    Lists files and directories sorted by their actual size.

Specifying Block Size

  • Specify block size (e.g., 1MB blocks):

    du -BM
    

    Shows disk usage in megabyte blocks. du -BG for gigabytes.

  • Combine with human-readable (shows block size in KB, MB, GB):

    du -h -k
    

    Shows sizes in kilobytes (default for -k). -m for megabytes, -g for gigabytes.

Excluding Directories

  • Exclude a specific directory from the scan:

    du -h --exclude='node_modules'
    

    Calculates disk usage for the current directory, skipping anything named node_modules.

  • Exclude multiple directories:

    du -h --exclude='node_modules' --exclude='*.log'
    

    Excludes directories named node_modules and any files ending in .log.

Common Patterns

  • Find the top 10 largest directories in the home folder:

    du -h --max-depth=1 /home/user | sort -rh | head -n 10
    
  • Find the largest files (not just directories) in the current directory:

    du -a . | sort -rh | head -n 10
    

    Note: du -a includes directories themselves.

  • Find the largest files (actual size) in the current directory:

    du -ah --apparent-size . | sort -rh | head -n 10
    
  • Check disk usage of web server logs:

    sudo du -sh /var/log/apache2
    
  • Check disk usage of Docker images (requires root/sudo):

    sudo du -sh /var/lib/docker/images/*
    

    (This path might vary based on Docker installation)

  • Find directories larger than a certain size (e.g., 1GB):

    du -h --max-depth=1 | awk '$1 ~ /[0-9.]+G/ && $1+0 > 1 {print $0}'
    

    This is a bit more advanced, using awk to filter lines where the size (first column) contains 'G' and is numerically greater than 1.

Gotchas

  • Permissions: du might report "permission denied" errors for directories you don’t have read access to. Use sudo du for a more complete picture if needed.
  • Block Size vs. Apparent Size: By default, du reports the disk space allocated for files, which can be larger than the actual file size due to filesystem block allocation. Use --apparent-size to see the actual file size. This is crucial for understanding the true size of sparse files.
  • Hidden Files/Directories: du by default only reports on directories. To see files as well, use the -a flag. Hidden files (starting with .) are included by default if -a is used.
  • Live Filesystems: du calculates usage based on the filesystem’s metadata. For very active filesystems with many writes, the reported size might not be perfectly instantaneous.
  • Mount Points: du by default will traverse into mounted filesystems. Use the -x (or --one-file-system) flag to restrict the scan to the filesystem of the starting directory.
    du -xh /
    
    This will calculate usage for the root filesystem only, not descending into other mounted partitions or network shares.