Introduction
The du
(Disk Usage) command in Linux is a powerful tool used to check the size of files and directories. This command is particularly useful for system administrators and users who want to monitor disk space usage efficiently. In this guide, we will explore all the options available with du
and provide practical examples.
Syntax
The basic syntax of the du
command is:
du [OPTIONS] [FILE/DIRECTORY]
If no file or directory is specified, du
reports the disk usage of the current directory.
Commonly Used Options
Here’s a breakdown of useful du
options with detailed explanations and examples.
1. Display Disk Usage in Human-Readable Format
du -h
This option provides output in a human-readable format, using KB, MB, or GB.
Example:
$ du -h /home/user
4.0K /home/user/Documents
20M /home/user/Downloads
2.3G /home/user/Videos
2. Summarize the Total Size of a Directory
du -sh /path/to/directory
The -s
option provides only the total size of the specified directory.
Example:
$ du -sh /var/log
3.5G /var/log
3. Display Sizes of All Files and Subdirectories
du -a /path/to/directory
This option includes all files along with directories.
Example:
$ du -a /home/user
12K /home/user/.bashrc
4.0K /home/user/.profile
20M /home/user/Downloads
4. Show Disk Usage in Specific Units
du -BM /path/to/directory
This forces du
to display output in Megabytes.
Example:
$ du -BM -d 1 /home/user
1M /home/user/Documents
50M /home/user/Downloads
5. Exclude Certain Files or Directories
du --exclude='*.log' /path/to/directory
This excludes files matching a specific pattern.
Example:
$ du -sh --exclude='*.log' /var/log
2.8G /var/log
6. Limit Depth of Directory Traversal
du -d N /path/to/directory
Where N
is the depth level.
Example:
$ du -h -d 1 /home/user
4.0K /home/user/Documents
20M /home/user/Downloads
7. Show Apparent Size Instead of Disk Usage
du --apparent-size -h /path/to/directory
This option displays the actual file size instead of blocks allocated on the disk.
Example:
$ du --apparent-size -h /home/user
8. Display Disk Usage of Multiple Directories
du -ch /dir1 /dir2
This sums up the disk usage of multiple directories and shows a total.
Example:
$ du -ch /etc /var
9. Sorting Output by Size
du -ah /path/to/directory | sort -rh
This sorts the output from largest to smallest.
Example:
$ du -ah /home/user | sort -rh | head -10
10. Checking File System Usage in a Mounted Partition
du -xh /mount/point
This excludes files from other mounted file systems.
Example:
$ du -xh /
Conclusion
The du
command is an essential tool for managing disk usage in Linux. By combining different options, you can efficiently analyze storage consumption, identify large files, and optimize disk space. Mastering du
will help you maintain a well-organized and efficient Linux system.
For further reading, check out the man
page:
man du