Knowing how much space a file or folder consumes on a partition is essential for a system administrator or developer. This knowledge allows you to plan for storage upgrades, manage and rotate files, and do other necessary sysadmin tasks. My favorite command for this type of data gathering is the du command.

The du command summarizes disk usage of each file and recursively for each directory. It offers many helpful options individually or in the correct combinations. For all of the options, refer to the du man page. Here is one of my favorite tricks with du.

Use du

I usually like to check the usage of multiple directories simultaneously. This way, I know which directories are my biggest offenders. Suppose I want to check all directories in /var. Here's my standard du command, with long options for clarity:

$ du --all --human-readable \
--one-file-system \
--max-depth=1 /var

Here is a breakdown of each option (with the short version in parentheses):

  • --all (-a): Print all files and folders.
  • --human-readable (-h): Print sizes in big chunks rather than in bytes (for instance, 1K instead of 1024).
  • --one-file-system (-x): Skip directories on different filesystems. The result is that if /var/log is mounted separately, it isn't counted because it's on a separate filesystem. This ensures I see the disk space used under only one directory path and not across physical media.
  • --max-depth=1 (-d): Print the total for a directory (or file, with --all) only if it is, in this case, one level below /var. If you use 2 instead, it prints folders two levels below /var. Unlike the --one-file-system option, the size reported remains the same with this option; I just don't have to see as much output.

Here's the sample output from my usual du command:

$ du -ahx --max-depth=1 /var
0       /var/lock
0       /var/mail
0       /var/run
12K     /var/kerberos
12K     /var/sieve
135M    /var/spool
1.6G    /var/log
181M    /var/cache
20K     /var/db
336K    /var/named
3.7G    /var/vmail
4.0K    /var/adm
4.0K    /var/crash
4.0K    /var/ftp
4.0K    /var/games
4.0K    /var/gopher
4.0K    /var/local
4.0K    /var/nis
4.0K    /var/opt
4.0K    /var/preserve
4.0K    /var/.updated
4.0K    /var/yp
4.2G    /var/lib
44K     /var/tmp
8.0K    /var/empty
9.7G    /var

While this output is good, it would be even better to sort it by capacity. That way, it's even easier to read at a glance.

[ Check out 10 tutorials to sharpen your command-line skills. ]

The trick: Sort the output

The good news is I can sort the output in any order I want by passing sort -k1 -rh as input. For example, here's what I get when I run a command to sort the output by the first column (capacity):

$ du -ahx --max-depth=1 /var | sort -k1 -rh
9.7G    /var
4.2G    /var/lib
3.7G    /var/vmail
1.6G    /var/log
181M    /var/cache
135M    /var/spool
336K    /var/named
44K     /var/tmp
20K     /var/db
12K     /var/sieve
12K     /var/kerberos
8.0K    /var/empty
4.0K    /var/yp
4.0K    /var/.updated
4.0K    /var/preserve
4.0K    /var/opt
4.0K    /var/nis
4.0K    /var/local
4.0K    /var/gopher
4.0K    /var/games
4.0K    /var/ftp
4.0K    /var/crash
4.0K    /var/adm
0       /var/run
0       /var/mail
0       /var/lock

Monitor disk usage

There are also graphical tools to check disk space, such as the ncdu command, but for me, the du command is simple, direct, and efficient. I hope that this quick tip helps you level up your sysadmin fundamentals. These options have made gathering file capacity data much easier for me, and when entered as an alias in your .bashrc, it becomes second nature.


关于作者

I work as Unix/Linux Administrator with a passion for high availability systems and clusters. I am a student of performance and optimization of systems and DevOps. I have passion for anything IT related and most importantly automation, high availability, and security.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来