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 use2
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.
Über den Autor
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.
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Original Shows
Interessantes von den Experten, die die Technologien in Unternehmen mitgestalten