Linux du command

The du command in Linux is used to estimate and display disk usage of files and directories. It helps you determine the space occupied by files and directories on your storage device.

Here’s an overview of how to use the du command:

  1. Basic Usage:
    To use du, simply type du followed by the path of the file or directory you want to analyze. For example:
   du /path/to/directory

This command will display the disk usage of the specified directory, including the disk space occupied by its subdirectories and files.

  1. Displaying Human-Readable Sizes:
    By default, du displays the disk usage in kilobytes. You can use the -h option to display sizes in a more human-readable format, such as kilobytes (K), megabytes (M), or gigabytes (G). For example:
   du -h /path/to/directory

This command will display the disk usage with sizes in a human-readable format.

  1. Summary Total:
    To display the total disk usage of a directory and its subdirectories without listing each individual file, you can use the -s (summary) option. For example:
   du -sh /path/to/directory

This command will display the total disk usage of the directory in a human-readable format.

  1. Sorting by Disk Usage:
    By default, du lists files and directories in alphabetical order. You can use the --sort option to sort the output based on disk usage. For example, to list the largest files or directories first, you can use the --sort=-n option. For example:
   du -sh --sort=-n /path/to/directory

This command will display the disk usage of files and directories in descending order, with the largest ones listed first.

  1. Examples:
  • Displaying disk usage of a directory: du /path/to/directory This command displays the disk usage of the specified directory and its subdirectories.
  • Displaying disk usage in human-readable format: du -h /path/to/directory This command displays the disk usage of the specified directory with sizes in a human-readable format.
  • Displaying total disk usage of a directory: du -sh /path/to/directory This command displays the total disk usage of the specified directory in a human-readable format.
  • Sorting by disk usage:
    du -sh --sort=-n /path/to/directory
    This command displays the disk usage of files and directories in the specified directory, sorted in descending order of disk usage.

The du command is helpful for monitoring disk usage, identifying large files or directories that consume significant space, and managing storage capacity.

For more information about the du command and its options, you can refer to the manual page by typing man du in your terminal.