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

AI (人工知能)

お客様が AI ワークロードをどこでも自由に実行することを可能にするプラットフォームについてのアップデート

open hybrid cloud icon

オープン・ハイブリッドクラウド

ハイブリッドクラウドで柔軟に未来を築く方法をご確認ください。

security icon

セキュリティ

環境やテクノロジー全体に及ぶリスクを軽減する方法に関する最新情報

edge icon

エッジコンピューティング

エッジでの運用を単純化するプラットフォームのアップデート

Infrastructure icon

インフラストラクチャ

世界有数のエンタープライズ向け Linux プラットフォームの最新情報

application development icon

アプリケーション

アプリケーションの最も困難な課題に対する Red Hat ソリューションの詳細

Virtualization icon

仮想化

オンプレミスまたは複数クラウドでのワークロードに対応するエンタープライズ仮想化の将来についてご覧ください