Enhancing Skills

du: Estimate the disk usage of a directory and its subdirectories

Command: du

The du (disk usage) command estimates and displays the disk space used by files and directories. It provides information on how much space is used by a specified directory and its subdirectories.


Sample Commands and Outputs:

  • du: Displays the disk usage of the current directory and its subdirectories. Sample Command and Output:
  $ du
  4       ./dir1
  12      ./dir2/subdir
  16      ./dir2
  20      .

Description:

  • Displays the disk usage of each directory and subdirectory. The final line shows the total disk usage of the current directory.
  • du -h: Displays disk usage in human-readable format (e.g., KB, MB, GB). Sample Command and Output:
  $ du -h
  4.0K    ./dir1
  12K     ./dir2/subdir
  16K     ./dir2
  20K     .

Description:

  • -h: Human-readable format, showing sizes in KB, MB, GB, etc.
  • du -s: Displays the total disk usage of the specified directory only. Sample Command and Output:
  $ du -s /path/to/dir
  1234    /path/to/dir

Description:

  • -s: Summarizes the total disk usage of the specified directory, without listing individual subdirectories.
  • du -a: Includes all files and directories in the output. Sample Command and Output:
  $ du -a
  4       ./file1
  12      ./dir2/subdir/file2
  16      ./dir2/subdir
  20      ./dir2
  24      .

Description:

  • -a: Displays the disk usage for all files and directories.
  • du -c: Displays a grand total at the end of the output. Sample Command and Output:
  $ du -c
  4       ./dir1
  12      ./dir2/subdir
  16      ./dir2
  32      total

Description:

  • -c: Provides a grand total of the disk usage at the end of the output.
  • du -x: Excludes directories on different file systems. Sample Command and Output:
  $ du -x
  4       ./dir1
  12      ./dir2/subdir
  16      ./dir2
  20      .

Description:

  • -x: Limits the report to the current file system only, excluding other file systems.
  • du --max-depth=N: Limits the output to directories up to N levels deep. Sample Command and Output:
  $ du --max-depth=1
  4       ./dir1
  16      ./dir2
  20      .

Description:

  • --max-depth=N: Limits the depth of directory traversal to N levels.

Note: The du command is useful for monitoring disk space usage and managing storage. Using the appropriate options allows you to customize the output based on your needs.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.