du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and it is used to display the amount of disk space used by a file or directory, as well as the space used by its subdirectories. The du command is often used to determine which files or directories are taking up the most space on a system.

The basic syntax of the du command is:

du [options] [file or directory]

When executed without any options, the du command will display the amount of space used by the specified file or directory, as well as the space used by its subdirectories. For example, the following command will display the amount of space used by the directory "example":

du example

The most commonly used option for the du command is the "-h" option, which stands for "human-readable." This option causes the du command to display the space used in a more human-readable format, such as "1K" for 1 kilobyte, "1M" for 1 megabyte, etc. For example, the following command will display the amount of space used by the directory "example" in a human-readable format:

du -h example

Another useful option for the du command is the "-a" option, which stands for "all." This option causes the du command to display the space used by all files in a directory, including hidden files. For example, the following command will display the amount of space used by all files in the directory "example", including hidden files:

du -a example

The du command also has the option "-c" which can be used to display a total of the space used by all of the specified files and directories. This can be useful when trying to determine the total amount of space used by a group of files or directories. For example, the following command will display the total amount of space used by the directories "example1" and "example2":

du -c example1 example2

The du command can also be used with the option "-d" which allows you to specify the depth of the directory tree to be traversed. This can be useful when trying to limit the amount of information displayed by the du command. For example, the following command will display the space used by the top level files and directories in the "example" directory:

du -d 1 example

The du command can also be used with the option "-S" which can be used to exclude subdirectories from the disk usage calculation. This can be useful when trying to determine the disk usage of a specific file or directory without including the disk usage of its subdirectories.

du -S example

The du command can also be used with the option "-x" which can be used to limit the disk usage calculation to the file system that the specified file or directory resides on. This can be useful when trying to determine the disk usage of a specific file or directory on a specific file system.

du -x example

The "du" command is a very useful command for monitoring disk usage in Linux and other Unix-like operating systems. Here are some additional features and uses of the "du" command:

  • The "du" command can also be used with the "-b" option which can be used to display the disk usage in bytes. This can be useful when trying to determine the exact number of bytes used by a file or directory.
    • du -b example
  • The "du" command can also be used with the "-l" option which can be used to count the disk usage of symbolic links as the disk usage of the files they point to, rather than the disk usage of the links themselves.
    • du -l example
  • The "du" command can also be used with the "-L" option which can be used to count the disk usage of symbolic links as the disk usage of the links themselves, rather than the disk usage of the files they point to.
    • du -L example
  • The "du" command can also be used with the "-P" option which can be used to avoid traversing mount points. This can be useful when trying to determine the disk usage of a specific file or directory without including the disk usage of any mounted file systems.
    • du -P example
  • The "du" command can also be used with the "-r" option which can be used to include the disk usage of subdirectories and files that have been deleted but still have a file descriptor open.
    • du -r example

In summary, the "du" command is a versatile command that can be used in many ways to estimate the space used by a file or directory in Linux and other Unix-like operating systems. It can be used with various options like -b, -l, -L, -P, -r to handle different scenarios and display the disk usage in bytes, count the disk usage of symbolic links, avoid traversing mount points and include deleted files. The "du" command is an essential tool for system administrators, developers, and other users to monitor disk usage, identify large files or directories, and help keep disk usage in check.