# DU (Disk Usage) Commands: A Comprehensive Guide
The 'du' (disk usage) command is a standard Unix/Linux utility used to estimate file and directory space usage. This guide covers the basics and advanced usage of the 'du' command, helping you efficiently manage your disk space.
## Common DU Flags
Before diving into specific commands, here's a quick reference for the most commonly used du flags:
- `-h`: Human-readable output (e.g., 1K, 234M, 2G)
- `-s`: Display only a total for each argument
- `-c`: Produce a grand total
- `-a`: Display an entry for each file, not just directories
- `-x`: Skip directories on different file systems
- `--max-depth=N`: Print the total for a directory only if it is N or fewer levels below the command line argument
- `-b`: Print size in bytes
- `-k`: Print size in kilobytes (1024 bytes)
- `-m`: Print size in megabytes
- `--exclude=PATTERN`: Exclude files that match PATTERN
These flags can be combined as needed. For example, `-sh` gives a human-readable summary.
## Basic DU Usage
- **Check Disk Usage of Current Directory:**
`du`
Displays disk usage for the current directory and all subdirectories.
- **Check Disk Usage of Specific Directory:**
`du /path/to/directory`
Displays disk usage for the specified directory and all its subdirectories.
- **Human-Readable Output:**
`du -h /path/to/directory`
Displays disk usage in human-readable format (e.g., 1K, 234M, 2G).
- **Summary of Disk Usage:**
`du -sh /path/to/directory`
Displays only the total disk usage of the specified directory in human-readable format.
## Advanced DU Usage
- **Display Usage for All Files, Not Just Directories:**
`du -ah /path/to/directory`
Shows disk usage for all files and directories in human-readable format.
- **Limit Depth of Directory Tree:**
`du -h --max-depth=1 /path/to/directory`
Displays disk usage of directory and its immediate subdirectories, but not deeper levels.
- **Sort Output by Size:**
`du -h /path/to/directory | sort -h`
Lists disk usage of all subdirectories, sorted by size (requires GNU sort).
- **Display Top N Largest Directories/Files:**
`du -ah /path/to/directory | sort -rh | head -n 5`
Shows the 5 largest items in the specified directory.
- **Exclude Certain File Types:**
`du -h --exclude="*.txt" /path/to/directory`
Calculates disk usage excluding all .txt files.
- **Calculate Total Size of Multiple Directories:**
`du -sch /dir1 /dir2 /dir3`
Displays a summary of each specified directory and a grand total.
## Combining DU with Other Commands
- **Find Large Files:**
`find /path/to/directory -type f -exec du -h {} + | sort -rh | head -n 20`
Lists the 20 largest files in the specified directory.
- **Disk Usage by File Type:**
`find /path/to/directory -type f | sed -e 's/.*\.//' | sort | uniq -c | sort -rn`
Shows a count of files by extension, sorted by frequency.
## Tips for Using DU
- **Use `-x` to Stay on One Filesystem:**
When checking disk usage of system directories, use `du -xh /` to avoid including mounted filesystems.
- **Combine with `watch` for Real-Time Monitoring:**
`watch -n 10 'du -sh /path/to/directory'`
Updates the disk usage of the specified directory every 10 seconds.
- **Use `--time` to See Last Modified Time:**
`du -sh --time /path/to/directory`
Shows the disk usage along with the last modification time of each subdirectory.
## Note on Accuracy
The 'du' command calculates apparent disk usage, which may differ from the actual disk space consumed due to factors like file system block size, sparse files, and hard links. For exact disk usage, refer to the file system's specifications or use specialized tools.
This guide covers essential and advanced 'du' commands for analyzing disk usage. As you become more familiar with these operations, they will significantly enhance your ability to manage disk space and identify large files or directories consuming your storage.