du: Estimating File Space Usage

The du (disk usage) command is a standard Unix utility used to estimate file space usage. It summarizes the disk space used by files and directories specified by the arguments, helping you identify where disk space is being consumed on your system.

1. Summarize Disk Usage for Current Directory (Human-Readable)

This command provides a human-readable summary of disk usage for all top-level files and directories (excluding hidden ones) within your current working directory.

du -sh * | sort -h
  • du: The disk usage command.
  • -s: (summary) Displays only a total for each argument.
  • -h: (human-readable) Displays sizes in powers of 1024 (e.g., K, M, G).
  • *: A wildcard that expands to all non-hidden files and directories in the current directory.
  • | sort -h: Pipes the output to sort -h, which sorts the results by human-readable size, making it easy to see the largest consumers of disk space.

2. Including Hidden Files and Folders

To get a summary that also includes hidden files and directories (those starting with a .), you need to explicitly include them.

du -sh ~/.* ~/ | sort -h
  • ~/.*: Matches all hidden files and directories in your home directory.
  • ~/: Matches all non-hidden files and directories in your home directory.

3. Other Common du Usage Examples

Summarize Disk Usage for a Specific Path

du -sh <path>
# Example:
du -sh /var/log

Show Disk Usage for All Files and Subdirectories

This will list the size of every file and directory within the specified path.

du -h <path>
# Example:
du -h /etc

Limit Recursion Depth

To see the disk usage of subdirectories up to a certain level.

du -h --max-depth=1 <path>
# Example: Show disk usage of direct subdirectories in current location
du -h --max-depth=1 .

Exclude Specific File Types or Patterns

du -h --exclude="*.log" /var/log

Tip: du calculates disk usage by traversing the file system. For very large file systems, it can take some time to complete.