du - Linux


Overview

The du command (short for disk usage) is a command-line tool in Linux used to estimate the space used by files and directories on a filesystem. This tool is particularly handy for managing disk space, identifying large files, and cleaning up unused files.

Syntax

The basic syntax of the du command is:

du [OPTIONS]... [FILE]...

Where OPTIONS are command modifiers that alter the function of the command, and FILE is the directory or file paths to analyze disk usage. If no file or directory is specified, du will report the space used by the directory and its subdirectories of the current directory.

Options/Flags

Here are some commonly used options with the du command:

  • -a, --all: Include all files and directories when showing disk usage.
  • -c, --total: Produce a grand total of all the files and directories specified.
  • -h, --human-readable: Print sizes in human-readable format (e.g., 1K, 234M, 2G).
  • -s, --summarize: Display only a total for each argument.
  • -x, --one-file-system: Skip directories on different filesystems.
  • -k: List sizes in kilobytes. This is the default on some systems.
  • --max-depth=N: Limit the display of disk usage to N levels of depth.

Examples

Here are several examples that show how to use the du command:

  1. Basic Usage:
    Display disk usage for all directories and subdirectories in the current directory.

    du
    
  2. Human-Readable Sizes:
    Display sizes in a format easier to read (e.g., KB, MB, GB).

    du -h
    
  3. Summarize Output:
    Show only the total size of a directory (not its subdirectories).

    du -sh /var/log
    
  4. Include All Files:
    List the size for each file and directory:

    du -ah
    
  5. Using Depth Limit:
    Display usage up to three directory levels deep:

    du -h --max-depth=3
    
  6. Combine with Other Commands:
    Combine du with sort to find the largest files or directories:

    du -ah | sort -rh | head -n 10
    

Common Issues

  • Permission Denied: Running du without sufficient permissions can result in “Permission Denied” errors. Use sudo to run du and gain necessary permissions.
  • Output too Verbose: When the output is too detailed, use the -s option to limit the data to summarizable information.

Integration

The du command can be effectively combined with other command-line tools to manage files and monitor disk usage. For instance, using du with grep to filter specific types of files:

du -ah | grep '\.log$'

This combination is beneficial for monitoring log file sizes within a directory.

  • df: Display disk space usage of file systems.
  • sort: Sort lines of text files.
  • grep: Search for patterns in files.
  • head: Output the first part of files.

Further reading and more detailed information can be found in the GNU Coreutils documentation.