Linux du command


The du (Disk Usage) command in Linux is used to estimate and display the disk space used by files and directories. It provides a summary of disk usage for a given file or directory, helping users understand how much space is being used on the filesystem.

Basic Syntax:

du [options] [directory]

If no directory is specified, du will show the disk usage of the current directory.

Commonly Used Options:

  • -h: Human-readable format (shows sizes in KB, MB, GB, etc.).
  • -s: Display only the total for each argument (summarize).
  • -a: Show disk usage for all files, not just directories.
  • -c: Produce a grand total (shows total disk usage at the end).
  • -d <depth>: Limit the depth of directory traversal.
  • -m: Display disk usage in megabytes.
  • -k: Display disk usage in kilobytes (this is the default).

Basic Usage:

du

Example Output:

8 ./dir1 4 ./dir2 16 ./dir3 28 .
  • The du command by default shows the disk usage of each subdirectory in the current directory.
  • The total disk usage for the current directory (denoted by .) is 28 blocks.

Example with -h (Human-readable format):

du -h

Sample Output:

8.0K ./dir1 4.0K ./dir2 16K ./dir3 28K .
  • The sizes are now displayed in a human-readable format (KB, MB, GB), making it easier to interpret.

Example with -s (Summarize):

du -sh

Sample Output:

28K .
  • The -s option displays the total disk usage of the current directory and its contents, without showing individual subdirectories.

Example with -a (Show all files):

du -ah

Sample Output:

4.0K ./dir1/file1 4.0K ./dir1/file2 8.0K ./dir1 4.0K ./dir2/file1 4.0K ./dir2 16K ./dir3 28K .
  • The -a option shows the disk usage for all files, not just directories.

Example with -c (Grand total):

du -ch

Sample Output:

8.0K ./dir1 4.0K ./dir2 16K ./dir3 28K . 28K total
  • The -c option produces a grand total of disk usage at the end.

Example with -d <depth> (Limit depth of directories):

du -h -d 1

Sample Output:

8.0K ./dir1 4.0K ./dir2 16K ./dir3 28K .
  • The -d 1 option limits the depth of the directory listing to just one level, showing the disk usage of the directories directly under the current directory.

Example with -m (Display in Megabytes):

du -m

Sample Output:

1 ./dir1 1 ./dir2 2 ./dir3 4 .
  • The -m option displays the disk usage in megabytes.

Example with -k (Display in Kilobytes):

du -k

Sample Output:

8 ./dir1 4 ./dir2 16 ./dir3 28 .
  • The -k option is the default and displays disk usage in kilobytes (KB).

Example with du on a specific directory:

du -sh /path/to/directory

Sample Output:

1.4G /path/to/directory
  • The -sh options show the total size of the /path/to/directory in a human-readable format.

Conclusion:

The du command is useful for analyzing disk space usage and finding out which files and directories are taking up space. Using the various options such as -h for human-readable format, -s for summarizing, and -a for showing all files, you can customize the output to your needs. It's particularly helpful for system administrators and users trying to manage storage effectively.