How to use the command 'du' (with examples)

How to use the command 'du' (with examples)

  • Osx
  • December 25, 2023

The ‘du’ command is used to estimate and summarize the file and directory space usage on a system. It provides information about the sizes of directories and files, and can be used to track disk usage.

Use case 1: List the sizes of a directory and any subdirectories, in the given unit (KiB/MiB/GiB)

Code:

du -k|m|g path/to/directory

Motivation: This use case is useful when you want to know the sizes of a directory and its subdirectories, and you want to specify the unit in which you want the sizes to be displayed (KiB, MiB, or GiB).

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -k|m|g: This option is used to specify the unit of the sizes to be displayed. -k is for KiB (kilobytes), -m is for MiB (megabytes), and -g is for GiB (gigabytes).
  • path/to/directory: The path to the directory for which you want to list the sizes and its subdirectories.

Example output:

$ du -k /path/to/directory
1024    path/to/directory
512     path/to/directory/subdirectory1
2048    path/to/directory/subdirectory2

Use case 2: List the sizes of a directory and any subdirectories, in human-readable form

Code:

du -h path/to/directory

Motivation: This use case is handy when you want to view the sizes of a directory and its subdirectories in a human-readable format, where the size is automatically selected based on its magnitude (e.g., KB, MB, GB).

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -h: This option is used to display the sizes in a human-readable format.
  • path/to/directory: The path to the directory for which you want to list the sizes and its subdirectories.

Example output:

$ du -h /path/to/directory
1.0K    path/to/directory
512B    path/to/directory/subdirectory1
2.0K    path/to/directory/subdirectory2

Use case 3: Show the size of a single directory, in human-readable units

Code:

du -sh path/to/directory

Motivation: This use case is useful when you only want to see the size of a single directory in a human-readable format and not the subdirectory sizes.

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -s: This option is used to display only the total size of the specified directory.
  • -h: This option is used to display the size in a human-readable format.
  • path/to/directory: The path to the directory for which you want to display the size.

Example output:

$ du -sh /path/to/directory
1.0K    path/to/directory

Use case 4: List the human-readable sizes of a directory and of all the files and directories within it

Code:

du -ah path/to/directory

Motivation: This use case is helpful when you want to display the sizes of both the directory and all the files and directories within it in a human-readable format.

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -a: This option is used to include individual files and directories in the output, in addition to the size of the specified directory.
  • -h: This option is used to display the sizes in a human-readable format.
  • path/to/directory: The path to the directory for which you want to list the sizes and its contents.

Example output:

$ du -ah /path/to/directory
1.0K    path/to/directory
512B    path/to/directory/file1.txt
2.0K    path/to/directory/directory1

Use case 5: List the human-readable sizes of a directory and any subdirectories, up to N levels deep

Code:

du -h -d 2 path/to/directory

Motivation: This use case is useful when you want to limit the depth of the subdirectories for which you want to display the sizes. For example, if you set -d 2, it will only show the sizes of the directory, its immediate subdirectories, and their contents.

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -h: This option is used to display the sizes in a human-readable format.
  • -d 2: This option is used to specify the number of levels deep you want to display the sizes for.
  • path/to/directory: The path to the directory for which you want to list the sizes and its subdirectories.

Example output:

$ du -h -d 2 /path/to/directory
1.0K    path/to/directory
512B    path/to/directory/subdirectory1
2.0K    path/to/directory/subdirectory2

Use case 6: List the human-readable size of all ‘.jpg’ files in subdirectories of the current directory, and show a cumulative total at the end

Code:

du -ch */*.jpg

Motivation: This use case is helpful when you want to check the size of all ‘.jpg’ files in the subdirectories of the current directory and get a cumulative total at the end.

Explanation:

  • du: The command for estimating and summarizing disk usage.
  • -c: This option is used to show a grand total at the end.
  • -h: This option is used to display the sizes in a human-readable format.
  • */*.jpg: File pattern that matches all ‘.jpg’ files in the subdirectories of the current directory.

Example output:

$ du -ch */*.jpg
256K    subdirectory1/file1.jpg
512K    subdirectory2/file2.jpg
768K    total

Conclusion:

The ‘du’ command is a versatile tool for estimating and summarizing disk usage. It can be used to list the sizes of directories, subdirectories, and individual files. The options available provide flexibility to display the sizes in different units and formats, making it easier to track and manage disk space.

Tags :

Related Posts

How to use the command "checkupdates" (with examples)

How to use the command "checkupdates" (with examples)

“checkupdates” is a command tool used in Arch Linux to check for pending updates.

Read More
Unmount Command (with examples)

Unmount Command (with examples)

The umount command is a versatile tool for unlinking a filesystem from its mount point, rendering it inaccessible.

Read More
How to use the command git worktree (with examples)

How to use the command git worktree (with examples)

The git worktree command is used to manage multiple working trees attached to the same repository.

Read More