try.directtry.direct

The power of du

The du (disk usage) command is a powerful and versatile tool in Unix-based operating systems, providing users with insights into how disk space is being used. This cookbook will guide you through various examples and practical uses of the du command, helping you master its features for effective disk management.


Introduction to du


The du command, short for disk usage, estimates the file space usage of a directory or files. It can display the disk usage for each file and directory recursively, providing valuable insights into which files and directories are consuming the most space.


Basic Usage


The simplest way to use du is by running it without any options:


du /home/path/to/dir

bash


This command will list the disk usage of each file and subdirectory within the specified directory.


Example:


du /home/user/documents

Human-Readable Output


To make the output easier to read, especially for large files, use the -h (human-readable) option:


du -h /path/to/directory

bash


Example:

du -h /home/user/documents

This command will display the disk usage in kilobytes (K), megabytes (M), and gigabytes (G).


Summarizing Disk Usage


To get a summary of the total disk usage for a directory, use the -s (summarize) option:

du -s /path/to/directory

Combine it with the -h option for a human-readable summary:

du -sh /path/to/directory

Example:

du -sh /home/user/documents

Displaying Disk Usage for All Files and Directories


To see the disk usage for all files and directories within a specified directory, use the -a (all) option:


du -a /path/to/directory

Combine it with the -h option for a human-readable format:

du -ah /path/to/directory

Example:

du -ah /home/user/documents

Sorting Disk Usage


To identify the largest files and directories, you can sort the output by size. Use the sort command in combination with du:

du -h /path/to/directory | sort -h

To display only the top N largest files and directories, use the head command:

du -h /path/to/directory | sort -h | head -n 10

Example:

du -h /home/user/documents | sort -h | head -n 10

Excluding Specific Files or Directories


To exclude specific files or directories from the du output, use the --exclude option:

du -h --exclude="*.log" /path/to/directory

Example:

du -h --exclude="*.tmp" /home/user/documents

This command will exclude all files with a .tmp extension from the disk usage calculation.


Combining du with Other Commands


The du command can be combined with other commands to create powerful scripts for disk usage analysis. For example, you can use du with grep to filter the output:

du -ah /path/to/directory | grep "specific_pattern"

Example:

du -ah /home/user/documents | grep ".pdf"

This command will display the disk usage for all PDF files in the specified directory.


Generating Disk Usage Reports


To generate a disk usage report and save it to a file, redirect the output of du to a text file:

du -h /path/to/directory > disk_usage_report.txt

Example:

du -h /home/user/documents > disk_usage_report.txt

You can then view or analyze the report as needed.


And lastly myfavorite and commonly used, list files and directories with sizes from the current directory:


du -khs *

Shell

-k like --block-size=1K
-h print sizes in human readable format

-s summarize, display only a total for each argument


Example:

root@server:/var/log# du -khs *
0	alternatives.log
4.0K	alternatives.log.1
4.0K	alternatives.log.10.gz
136K	apt
344K	auth.log
544K	auth.log.1

Shell



Conclusion

The du command is an essential tool for managing disk space on Unix-based systems. By mastering its various options and combining it with other commands, you can efficiently monitor and analyze disk usage, ensuring optimal use of your storage resources.

Explore these examples and experiment with the du command to discover more ways to leverage its power for your disk usage analysis needs. Happy computing!