How to Check Directory and File Sizes in Linux Terminal Using du

When monitoring disk capacity, troubleshooting low-storage alerts, or cleaning up bloated directories on Linux servers, finding out which directories are consuming the most gigabytes is an everyday administrative task. While the df command reports overall filesystem and partition capacity, it cannot tell you which specific folders are responsible for the consumption. The fundamental Linux utility du (Disk Usage) recursively traverses directory trees to report precise, human-readable storage sizes across files, subdirectories, and mounts.

Understanding the Core du Command

By default, executing du without arguments outputs raw block counts for every single subdirectory in your current path. To make the tool genuinely useful for everyday troubleshooting, you combine it with specific diagnostic flags:

  • -h (human-readable): Converts raw byte blocks into intuitive units like Kilobytes (K), Megabytes (M), and Gigabytes (G).
  • -s (summary): Calculates and outputs only the grand total size of the target directory without printing every nested child folder.
  • -c (grand total): Appends a summary total line at the bottom of the output.
  • -a (all): Evaluates individual files as well as directory trees.

1. Checking the Total Size of a Specific Directory

To check the overall storage footprint of a specific folder without flooding your screen with subfolders:

du -sh /var/log

The terminal will print a clean, single-line summary—such as 4.2G /var/log—giving you the total size in seconds.

2. Listing Immediate Subfolder Sizes (Controlling Depth)

If you want to inspect each folder in a directory tree without recursing infinitely down into sub-subfolders, use the --max-depth parameter:

du -h --max-depth=1 /var

This prints the individual size of every directory immediately inside /var (such as /var/log, /var/cache, and /var/lib) while grouping their children into the parent total.

3. Finding and Sorting the Top 10 Largest Folders

To identify the biggest disk hogs in your current directory, pipe the du output into the sort command:

du -h --max-depth=1 . | sort -hr | head -n 10

Here is what happens in this pipeline:

  • sort -h: Sorts human-readable numbers (understanding that 2G is larger than 500M).
  • -r: Reverses the sort order so the largest directories appear at the very top.
  • head -n 10: Restricts the display to the top 10 largest folders.

4. Checking Apparent Size vs Disk Allocation

On modern filesystems with sparse files, compression, or deduplication, a file’s nominal size can differ substantially from the actual physical disk space it occupies:

  • Default du: Measures actual physical disk blocks allocated on storage.
  • du --apparent-size -h file.img: Measures the apparent length of the data inside the file, equivalent to the byte count reported by ls -l.

5. Excluding Specific Patterns and Hidden Directories

When auditing large software development projects or web roots, you can ignore dependencies or version control histories using the --exclude flag:

du -sh --exclude=".git" --exclude="node_modules" ~/projects/*

This skips bulky cache repositories and focuses your audit on primary application files.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.