How to Use the du -h Command for Disk Usage
The du
(Disk Usage) command in Linux and other Unix-like operating systems is a powerful tool for analyzing disk space usage. The -h
flag, short for “human-readable,” enhances the output, making it easier for users to understand disk usage without extensive conversions. This guide will cover everything you need to know about the du -h
command, from basic usage to advanced applications.
What Is the du
Command?
The du
command is used to estimate the disk space used by files and directories. By default, it outputs the size of each file and directory in blocks, which can be less intuitive for many users. Adding the -h
option simplifies this by displaying sizes in kilobytes (K), megabytes (M), gigabytes (G), etc.
Syntax:
du [options] [file/directory]
Key Features of the du
Command:
- Provides a summary of disk usage.
- Works recursively for directories.
- Supports customization via flags and options.
Basic Usage of du -h
The -h
option makes the output of du
more readable by converting raw byte counts into human-readable formats.
Example:
du -h /path/to/directory
Output:
4.0K /path/to/directory/file1
8.0K /path/to/directory/file2
12K /path/to/directory
Key Points:
- Sizes like
4.0K
or12K
represent disk usage in kilobytes (K). - By default,
du
lists disk usage for every file and subdirectory.
Commonly Used Options with du -h
1. Summarize Total Disk Usage
To get a single total for the directory:
du -sh /path/to/directory
Output:
12K /path/to/directory
- The
-s
flag summarizes the total usage. - Adding
-h
ensures human-readable output.
2. List Disk Usage for All Subdirectories
If you want details for all subdirectories:
du -h /path/to/directory
Output:
4.0K /path/to/directory/subdir1
8.0K /path/to/directory/subdir2
12K /path/to/directory
3. Display Usage for Specific File Types
To filter specific file types using du
with find
:
find /path/to/directory -name "*.txt" -exec du -h {} \;
Output:
4.0K /path/to/directory/file1.txt
8.0K /path/to/directory/file2.txt
4. Exclude Specific Files or Directories
To exclude certain directories:
du -h --exclude="*.log" /path/to/directory
Output:
4.0K /path/to/directory/subdir1
8.0K /path/to/directory/subdir2
12K /path/to/directory
- The
--exclude
option omits files or directories matching the pattern.
Advanced Applications of du -h
1. Sorting Disk Usage
To sort directories by size:
du -h /path/to/directory | sort -h
Output:
4.0K /path/to/directory/subdir1
8.0K /path/to/directory/subdir2
12K /path/to/directory
- The
sort -h
command sorts human-readable numbers correctly.
2. Analyzing Large Directories
To identify the largest directories:
du -h /path/to/directory | sort -rh | head -n 10
Output:
12K /path/to/directory
8.0K /path/to/directory/subdir2
4.0K /path/to/directory/subdir1
- The
-r
option reverses the sort order. - Use
head -n 10
to display the top 10 results.
3. Combining du
with xargs
For more complex filtering:
find /path/to/directory -type d | xargs du -h | sort -h
- This combination finds directories and sorts them by size.
Practical Scenarios for Using du -h
Cleaning Up Disk Space
When your system is low on disk space, use du -h
to find and remove large, unnecessary files or directories.
du -h --max-depth=1 /home/user
Output:
200M /home/user/Documents
1.5G /home/user/Downloads
3.2G /home/user
Generating Reports
For system administrators, du -h
is invaluable for generating disk usage reports. Combine it with cron jobs to monitor disk usage regularly.
Example cron job:
0 2 * * * du -h /var > /home/admin/disk_usage_report.txt
Limitations of du -h
While du -h
is versatile, it has some limitations:
- Slow on Large Filesystems: Scanning large directories can take time.
- Filesystem Boundaries: By default,
du
does not respect filesystem boundaries. Use the-x
option to restrict it to the current filesystem.du -h -x /mnt/data
- Sparse Files:
du
reports the actual disk space used, not the logical size. For sparse files, this can be misleading.
Conclusion
The du -h
command is an essential tool for managing disk space in Linux. By understanding its options and combining it with other commands, you can efficiently analyze and optimize disk usage. Whether you’re troubleshooting storage issues or generating detailed reports, du -h
is a reliable and versatile solution. Mastering its usage can significantly improve your system administration and disk management skills.