du Command in Linux

Topics Covered

Overview

The du (disk usage) command is a command-line utility that allows you to estimate the space used by files and directories in Linux. This article provides an introduction to the du command, its syntax, options, and example usages.

Linux du Command Syntax

The syntax for the du command is as follows:

Where:

  • options: Optional flags that alter the behavior of the du command.
  • directory: The directory or file to estimate disk usage for.

du Command Options:

  • -h: Displays sizes in a human-readable format.
  • -s: Displays only the total size of the specified directory.
  • -a: Displays the size of all files, not just directories.
  • --max-depth: Limits the depth of the directory tree to the specified level.

Example Usages

  • Estimating disk usage of the current directory:

    Explanation: Displays the estimated disk usage for the current directory and its subdirectories.

  • Estimating disk usage in human-readable format:

    Explanation: Displays the estimated disk usage for the current directory and its subdirectories in a human-readable format (e.g., 1K, 234M, 2G).

Tips

  • Use 'du -sh' to display only the total size of the specified directory in a human-readable format.

  • Use 'du -ah' to display the size of all files and directories in a human-readable format.

Advanced Use Cases of du Command in Linux

  • Estimating disk usage of a specific directory:

    Explanation: Displays the estimated disk usage for the specified directory and its subdirectories.

  • Limiting the depth of the directory tree:

    Explanation: Displays the estimated disk usage for the current directory and its immediate subdirectories in a human-readable format.

  • Sorting the output by size:

    Explanation: Displays the estimated disk usage for the current directory and its subdirectories in a human-readable format, sorted by size in descending order.

Conclusion

  • The du command is used to estimate the space used by files and directories in Linux.

  • Several options are available to modify the output, such as -h for human-readable format and -s for displaying only the total size.

  • Advanced usages include specifying a directory, limiting the depth of the directory tree, and sorting the output by size.