How to Check the Size of a Directory in Linux

When managing a Linux server, storage space is a premium resource. If you receive an alert that your primary partition is reaching 90% capacity, you need to quickly identify which folders are hoarding the data. Unlike graphical environments where you can right-click a folder and view its properties, analyzing disk usage in the Linux terminal requires specific tools. The du (Disk Usage) command is the standard utility for calculating the size of directories and their contents.

Understanding the ‘du’ Command

If you run the standard ls -l command to view a directory’s contents, the size listed next to a folder is usually 4096 bytes (4KB). This is misleading for beginners; 4KB is simply the size of the directory’s metadata block on the filesystem, not the combined size of the files stored inside it. To calculate the true size of a directory, the system must recursively scan every file within it and tally up the bytes. This is exactly what the du command does.

Step-by-Step: Checking a Directory’s Size

The basic du command provides a massive amount of output, so you almost always use it with specific flags to make the data readable.

  1. Open your terminal application or connect via SSH.
  2. To check the total size of a specific directory (for example, the /var/log folder), run the following command:
    du -sh /var/log

Let’s break down the flags used in this command:

  • -s (Summarize): By default, du prints the size of every single subfolder it encounters. The -s flag tells it to suppress that output and only display one grand total for the target directory.
  • -h (Human-readable): By default, du outputs the size in raw 1-kilobyte blocks, resulting in massive, confusing numbers. The -h flag translates that data into Megabytes (M), Gigabytes (G), or Terabytes (T), making it immediately understandable.

Advanced Usage: Finding the Largest Folders

Knowing the size of one specific folder is useful, but often you need to find out which subfolders within your current location are consuming the most space. You can do this by scanning the current directory and sorting the output.

  1. Navigate to the directory you want to analyze (e.g., cd /var).
  2. Run the following command:
    du -sh * | sort -hr

This command pipeline is incredibly powerful:

  • du -sh * calculates the summary size of every individual file and folder in the current directory.
  • | pipes that output into the next command.
  • sort -hr sorts the list. The -h tells the sort tool to understand human-readable sizes (so it knows 2G is larger than 500M), and the -r reverses the order, putting the largest folders at the very top of your screen.

Troubleshooting Common Mistakes

When using du, you may encounter permission issues or misleading data:

  • Permission Denied Errors: The du command must read every file to calculate its size. If you are a standard user trying to analyze a system directory (like /var or /root), you will receive a flood of “Permission denied” errors, and the final calculation will be inaccurate because those locked files were not counted. Always prepend your command with sudo (e.g., sudo du -sh /var) when analyzing system-level directories.
  • Apparent Size vs Disk Usage: du calculates the physical disk blocks consumed by a file. If you are dealing with “sparse files” (files that claim to be 100GB but only contain 1GB of actual data, common in virtual machine images), du might show a smaller number than expected. If you need to see the file’s stated size rather than its physical block usage, add the --apparent-size flag.

By mastering the du command and sorting its output, you can efficiently hunt down bloated log files and overgrown databases to keep your Linux servers running smoothly.

Get the best tech tips delivered straight to your inbox.

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