Blog / Linux/ Linux Disk Usage Guide: Mastering du and df Commands

Linux Disk Usage Guide: Mastering du and df Commands

Linux 查看目录与文件大小:du 与 df 命令详解与实用技巧

Understanding Disk Usage in Linux: A Guide to du and df Commands

Managing and monitoring disk space is a critical part of Linux system administration and development. The du (disk usage) and df (disk free) commands are essential tools for checking file/directory sizes and filesystem availability. This guide explains their common options, useful combinations, and practical applications.

1. Checking Directory Sizes with du

The du command estimates the disk space used by files and directories, recursively traversing subdirectories.

Basic Usage

  • View total size of current directory (human-readable):
    du -sh
  • View sizes of immediate subdirectories and files:
    du -h --max-depth=1
  • View sizes for a specific directory (e.g., /home):
    du -h --max-depth=1 /home
  • View sizes of all items in current directory:
    du -sh *

Advanced Techniques: Sorting and Filtering

Combine du with sort, head, tail, and grep to quickly identify large space consumers.

  • Sort by size (descending):
    du -sh /usr/* | sort -rh

    Note: -r reverses order (descending); -h allows sort to correctly interpret human-readable sizes (K, M, G).

  • Show top 10 largest directories:
    du -sh /usr/* | sort -rh | head -n 10
  • Show 10 smallest directories:
    du -sh /usr/* | sort -h | head -n 10
  • Find and sort directories in GB or TB range:
    du -h --max-depth=2 | grep -E '[0-9]+G|[0-9]+T' | sort -rh

2. Checking Filesystem Usage with df

The df command reports disk space usage for filesystems, showing total, used, and available space.

  • View all filesystems in human-readable format:
    df -h
  • View a specific filesystem or mount point (e.g., /home):
    df -h /home

3. Practical Scenarios: Cleanup and Deletion

After identifying space usage, cleanup is often necessary. Here are some safe deletion examples:

  • Delete all files in current directory (use with caution!):
    rm -f *
  • Delete contents of a specific directory (e.g., logs) but keep the directory:
    rm -rf /path/to/logs/*
  • Delete all files in current directory except a specific one (e.g., 'keep'):
    ls | grep -v keep | xargs rm -f

    For safer handling of complex filenames, use the find command.

4. Quick Reference Commands

Frequently used command combinations for quick reference:

# Show total size of current directory
du -sh
# Show sizes of immediate subdirectories, sorted
du -h --max-depth=1 | sort -h
# Find and sort large (GB/TB) directories
du -h --max-depth=2 | grep -E '[0-9]+G|[0-9]+T' | sort -rh
# View overall filesystem usage
df -h

Important Notes

  • Execution Context: du defaults to the current directory. Specify a path (e.g., du -sh /home) for other locations.
  • Permissions: Viewing some directories may require sudo privileges.
  • Symbolic Links: By default, du follows symbolic links. Use -L to force following, or -P to avoid it (default behavior may vary by version).
  • df vs. du Difference: df reports at the filesystem level (fast); du calculates from files (more accurate but slower, especially for deep directories).

Mastering du and df commands and their combinations enables efficient disk space monitoring, problem identification, and cleanup—essential skills for Linux system management and development.

Post a Comment

Your email will not be published. Required fields are marked with *.