
Mastering Disk Usage in Linux: A Practical Guide to the du Command
Running out of disk space is a common problem for system administrators and casual Linux users alike. When your storage fills up, performance can suffer, and critical services may fail. The first step to solving this problem is identifying what’s consuming your space. Fortunately, Linux provides a powerful command-line utility specifically for this task: du.
The du command, short for “disk usage,” is an essential tool for analyzing and managing file and directory sizes. This guide will walk you through its most useful functions, from basic checks to advanced techniques for pinpointing space-hogging culprits.
Understanding Basic Disk Usage with du
If you simply run the du command in a directory without any options, it will recursively scan all subdirectories and print a list of their sizes.
du /var/log
The output will show a long list of directories and their sizes listed in 1-kilobyte blocks, which isn’t very intuitive. To make the output immediately more useful, we can add an option for human-readable format.
The single most important option for du is the -h (human-readable) flag. This displays sizes in kilobytes (K), megabytes (M), and gigabytes (G), making the information much easier to understand at a glance.
du -h /var/log
This command is a significant improvement, providing a clear view of the disk space each subdirectory is using.
How to Get Summarized Totals for Directories
Often, you don’t need to see the size of every single subdirectory; you just want to know the total size of a specific directory. This is where the -s (summarize) flag comes in. It displays only the grand total for the specified directory.
For maximum efficiency, combine the -s and -h flags to get a summarized, human-readable total.
du -sh /home/username
This is one of the most frequently used combinations of the du command. It gives you a quick, clean answer to the question, “How big is this folder?”
You can also check multiple directories at once. To get a grand total for all the directories you listed, add the -c (total) flag.
du -shc /var/log /home/username /tmp
The command will output the individual totals for each directory and then provide a final “total” line at the end.
Finding the Largest Files and Directories
The real power of du comes from its ability to help you hunt down what’s taking up the most space. While you could manually scan the output, a more effective method is to combine du with other command-line tools like sort.
First, let’s control the depth of the scan. If you’re analyzing a directory with hundreds of subfolders (like /), you don’t want to see every single one. You can limit how many levels deep du will go with the --max-depth option. A depth of 1 is perfect for a high-level overview of a directory’s immediate contents.
du -h --max-depth=1 /var
This will show you the size of each directory located directly inside /var.
Now, for the most powerful technique: to find the largest directories, pipe the output of du into the sort command.
du -h --max-depth=1 / | sort -rh
Let’s break down this command:
du -h --max-depth=1 /: Calculates the human-readable disk usage for all directories at the root (/) of the filesystem, one level deep.|: This is the “pipe” operator. It sends the output of the first command as the input to the second command.sort -rh: This sorts the input it receives. The-rflag reverses the sort order (from largest to smallest), and the-hflag enables human-numeric sort (so it correctly sorts 1G as larger than 100M).
The result is a clean, sorted list showing you the largest directories at the top, allowing you to instantly identify where your disk space has gone.
Actionable Security and Usage Tips
Use
sudofor Accurate System-Wide Scans: When scanning system directories like/varor/, you will likely encounter “Permission denied” errors for files and folders your user account cannot access. This leads to an inaccurate report. Always usesudo(sudo du -sh /var) when you need a complete and accurate measurement of system-wide disk usage.Include Individual Files: By default,
duonly shows directory sizes. If you suspect large individual files are the problem (e.g., a massive log file), use the-a(all) flag to include them in the output.sudo du -ah --max-depth=1 /var/log | sort -rhRegularly Check Log Directories: System and application logs, commonly found in
/var/log, are notorious for growing uncontrollably. Regularly runningduin this directory can help you catch issues before they lead to a full disk.
By mastering the du command and its powerful options, you gain a critical skill for maintaining a healthy and efficient Linux system. Whether you’re cleaning up your personal machine or managing a production server, these techniques will empower you to quickly diagnose and resolve disk space issues.
Source: https://kifarunix.com/check-directory-usage-with-du-command-in-linux/


