Introduction
In the day-to-day of a system administrator or an advanced Linux user, it is essential to know how much space each directory or file occupies. The du (disk usage) command allows measuring disk space usage quickly and flexibly. Unlike df, which shows the available space on the filesystem, du focuses on the actual consumption of each path.
What is du
Du is a tool included in practically all Linux distributions. Its main function is to traverse a directory tree and sum the sizes of the files it contains, presenting the result in blocks or in human-readable units. It is especially useful when you need to identify which folders are consuming the most space before performing a cleanup.
Basic Syntax
The simplest way to run du is:
du [options] [path]
If no path is specified, du works on the current directory. By default, it shows the size of each subdirectory in blocks of 1024 bytes. To obtain a more friendly output, it is usually combined with the -h option.
Most Useful Options
- -h: prints sizes in human-readable format (K, M, G).
- -s: shows only the total for the specified directory, without breaking down subdirectories.
- -a: includes individual files in addition to directories.
- -c: adds a final line with the accumulated total.
- –max-depth=N: limits recursion depth to N levels.
- -x: stays within the same filesystem, avoiding crossing mount points.
Practical Examples
To view the human-readable size of the entire home directory:
du -h ~
If you only want the summarized total:
du -sh ~
To list the ten largest directories within /var:
du -h --max-depth=1 /var | sort -hr | head -10
To include files and obtain a final total:
du -ahc /usr/local | tail -1
To exclude a filesystem different from the root:
du -xh /
Tips and Tricks
Combine du with other commands like sort, head, or awk to create custom reports. For example, you can generate a weekly report on log growth:
du -h /var/log/* | sort -hr > /tmp/logs_report.txt
If you work in scripts, use the -b option to obtain exact sizes in bytes, which facilitates numerical comparisons.
Remember that du counts the space reserved by the filesystem, so in some cases the number may differ slightly from what df shows due to partially used blocks or inode reservation.
Conclusion
Mastering the du command is essential for anyone administering Linux systems. Its simplicity and power allow quickly detecting storage bottlenecks and making informed decisions about cleanup or disk expansion. Practicing with the described options and adapting them to your needs will make you more efficient in managing space.
This post is also available in ESPAÑOL.