How does the 'du' command handle subdirectories?

The du command (disk usage) estimates and reports the file space usage of directories and their subdirectories. When you run du on a directory, it recursively traverses through all its subdirectories and calculates the total disk space used by each one.

Here are some key points on how du handles subdirectories:

  1. Recursive Calculation: By default, du will include all subdirectories in its calculations. It sums up the sizes of files and directories within the specified directory.

  2. Human-Readable Format: Using the -h option, du displays the sizes in a human-readable format (e.g., KB, MB, GB).

  3. Depth Control: You can control how deep du goes into subdirectories using the --max-depth=N option, where N specifies the maximum depth of directories to display. For example, du -h --max-depth=1 will show the sizes of the immediate subdirectories only.

  4. Sorting Output: You can pipe the output of du to the sort command to organize the results by size, making it easier to identify which subdirectories are using the most space.

Example command to estimate space usage of all subdirectories in a directory:

du -h /path/to/directory

This command will list the size of each subdirectory and the total size of the specified directory.

0 Comments

no data
Be the first to share your comment!