How to Find the Size of a Directory in Ubuntu (du command)

When working on a headless Ubuntu server (or simply living in the terminal), you do not have the luxury of right-clicking a folder and selecting “Properties” to see how much space it takes up. If a server is running out of storage, you must use command-line tools to hunt down the bloated directories.

In the Linux ecosystem, the command used for checking file and folder storage is du (Disk Usage).

Here is how to use the du command to find the size of a specific directory, and how to sort the output so the largest folders appear at the bottom of your screen.

1. Check the Total Size of a Single Directory

If you just want to know the total size of one specific folder (for example, your /var/log directory), use the -s and -h flags.

du -sh /var/log

Here is what those flags mean:

  • -s (Summarize): Tells the command to only output the grand total for the specified directory, rather than listing the size of every single sub-folder inside of it.
  • -h (Human-readable): Converts the output from raw bytes into Megabytes (M) or Gigabytes (G) so it is actually readable (e.g., 2.4G instead of 2400000).

2. List the Sizes of All Folders in Your Current Directory

If you know your web root directory (/var/www/) is massive, but you do not know which website inside it is taking up the space, you can ask du to list the size of everything currently in front of you.

du -h --max-depth=1

The --max-depth=1 flag restricts the output to one level deep. This prevents the command from recursively printing hundreds of thousands of lines of code for every tiny file buried inside sub-directories. It keeps your terminal output clean and organized.

(Tip: You can change the 1 to a 2 to see the size of the immediate sub-folders as well).

3. Sort the Results from Smallest to Largest

If a directory contains 50 different folders, scanning the list with your eyes to find the largest one is tedious. Instead, you can “pipe” the output of the du command directly into the sort command.

du -h --max-depth=1 | sort -h

By appending | sort -h, you tell the system to sort the list numerically while respecting the human-readable formatting. (Without the -h flag on the sort command, Linux would assume a folder sized “900M” is larger than a folder sized “2G”, because 9 is larger than 2).

Sort Largest to Smallest

If you want the absolute largest folder to appear at the very top of your terminal output, simply add an r (reverse) flag to the sort command.

du -h --max-depth=1 | sort -hr

Dealing with “Permission Denied” Errors

When you run du on system directories (like /var/ or /etc/), you will likely see dozens of “Permission Denied” errors flood your screen. This is because your standard user account does not have read access to certain core system files, meaning du cannot calculate their size.

To get an accurate measurement of a system-level directory, you must run the command as a superuser by prepending sudo:

sudo du -sh /var/

Get the best tech tips delivered straight to your inbox.

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