Marcio Cunha

How to Check Directory and Subdirectory Size in the Terminal Using du

Learn how to audit disk space usage using the du command in Linux and macOS terminals. Discover how to filter results, summarize directories, and optimize storage efficiently.

Marcio Cunha12 min
Also available in:EspañolPortuguês
Summary
  • The du command calculates disk space usage of files and directories recursively within the terminal.
  • Combining the -s and -h flags summarizes the total size and displays values in human-readable formats.
  • Sorting results with utilities like sort helps quickly identify the largest storage consumers.
  • Accessing certain system directories requires administrator privileges through the sudo utility.
  • Automating this checking routine prevents surprises regarding sudden space exhaustion on servers and computers.

The Silent Challenge of Disk Space Exhaustion

Anyone who has ever encountered the dreaded warning message informing them that the hard drive is full knows how frustrating it can be. In Unix-based operating systems like Linux and macOS, data storage grows silently, and before we realize it, hundreds of gigabytes have been consumed by temporary files, forgotten logs, or old installers. Finding out exactly where that space went can feel like a herculean task for anyone used only to colorful graphical interfaces and endless windows.

In practice, this means opening the terminal black screen and resorting to utility commands designed to examine the file system from the ground up. While a visual file manager needs to load thumbnails and icons before calculating properties, the command line cuts straight to the point, processing metadata in fractions of second. Understanding how these tools work transforms the way you interact with your computer, providing true autonomy to diagnose hardware bottlenecks without relying on third-party software.

Understanding the Concept Behind the du Command

The acronym du stands for disk usage, whose main purpose is to estimate the space consumption of files and directories. Unlike other commands that list only the nominal file size, du reveals how much actual space that content is physically occupying on the hard drive or SSD blocks.

When executed without any parameters in a crowded folder, the utility enters a verbose mode and triggers an endless list on the screen, showing line by line the size of each individual file and subfolder. This default behavior usually frightens beginners because it generates a flood of data that is hard to read quickly. This is precisely where modifiers or flags come in, which are small codes we add next to the command to change its behavior and make it useful for daily tasks.

The Magic of the -sh Modifiers in Practice

To make the command output understandable for humans and not just machines, we use two magical letters right after the main command: s and h. Together, they form the famous expression du -sh, which solves ninety percent of everyday disk space auditing problems without requiring advanced programming knowledge.

The letter s stands for summarize. It instructs the command to display only the accumulated total of the directory you specified, ignoring the individual listing of hundreds of internal files inside it. Meanwhile, the letter h stands for human-readable. Instead of showing gigantic numbers in bytes—which require complex mental math to know if we are talking about megabytes or gigabytes—the tool automatically translates them into easy-to-read units, like 45M for megabytes or 2.1G for gigabytes.

Exploring Folders and Subfolders with Precision

Now that we know the fundamentals, let us get hands-on and analyze a real everyday situation. Imagine you want to know exactly how much space your documents folder or a software development project is taking up on your computer. To do this, simply open the terminal, type the basic instruction followed by the desired directory path, and press enter.

du -sh /path/to/your/folder

In practice, the terminal will process the request and display a single line containing the total size followed by the name of the analyzed directory. If you want to inspect all folders inside a specific directory without summarizing everything into a single number, you can replace the s letter with an asterisk or use other depth variations. This lets you quickly list which subfolders are draining your precious disk space.

Handling Permission Denied and Common Errors

During the routine of scanning directories in the terminal, it is extremely common to stumble upon permission denied error messages. This happens because the operating system protects certain vital system folders against improper reads by regular users, ensuring overall environment security and data integrity.

To bypass this obstacle when you genuinely need to audit a protected folder, the sudo utility comes into play, granting temporary superuser or administrator powers to the executed command. By typing sudo du -sh /var/log, for example, you request the system to perform the read while bypassing the usual security locks. However, use this power with caution, as executing administrative commands carelessly can alter the expected behavior of the operating system.

Combining Tools to Sort the Biggest Culprits

Knowing the size of a specific folder is useful, but the true power of the command line appears when we combine tools to solve complex problems in an automated way. Imagine you have a folder full of projects and want to discover the top five largest subdirectories consuming space, ordered from largest to smallest.

To achieve this goal, we use the concept of a pipeline, represented by the vertical bar character, which channels the output of one command into the input of another. We can combine du itself with the sort command to organize data numerically and with the head command to limit the display to just the first results in the list.

du -h --max-depth=1 /path/to/folder | sort -hr

This elegant combination transforms a chaotic listing into a clear, objective ranking, allowing you to make quick decisions about what to delete or move to an external backup. It is this kind of synergy between simple commands that makes the text interface so powerful for technology professionals and enthusiasts.

Final Thoughts on Terminal-Based Space Management

Mastering the du command and its variations represents a turning point in how we manage our computing resources, whether on a personal laptop or on remote cloud servers. The simplicity of the text-based interface hides impressive versatility, capable of saving hours of frustration in front of full disks and system warnings. With constant practice, these instructions cease to be memorized commands and become a natural part of your logical reasoning when solving infrastructure problems.

Ultimately, the ability to audit storage with precision returns full control of the machine to your hands. By understanding the fundamental concepts behind data blocks, user permissions, and utility combinations, you become a much more independent user, fully prepared to handle any technological glitch in your daily professional routine.