How to Check the Size of a Directory in Ubuntu Linux

The Storage Mystery

If your Ubuntu Linux server is suddenly warning you that the hard drive is 99% full, your first instinct is usually to type ls -l to look at file sizes. However, the standard list command only shows the size of individual files, not entire folders. If a rogue application has dumped 40 gigabytes of error logs deep inside /var/log/, the ls command will simply tell you that the /var/log/ directory itself takes up 4 kilobytes of space, which is entirely unhelpful. To find out exactly how much physical hard drive space an entire folder and all of its contents are consuming, you must use the “disk usage” command.

Using the du Command

The du command calculates the exact size of a directory by recursively diving into it and tallying up the size of every single file it finds.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To check the total size of the /var/log/ directory, type the following command:
  3. sudo du -sh /var/log/
  4. Press Enter and provide your administrator password.

Understanding the Flags

The terminal will instantly return a simple, highly readable output (e.g., 42G /var/log/). This is achieved through two specific flags:

  • -s (Summarize): Without this flag, the du command will print a scrolling wall of text, listing the size of every single sub-folder and file it encounters before giving you the final total. The -s flag forces the command to remain silent until it finishes calculating, printing only the final, grand total.
  • -h (Human Readable): Without this flag, the command prints the size in raw kilobytes (e.g., 44040192 /var/log/). The -h flag automatically converts the massive number into easily readable Gigabytes (G), Megabytes (M), or Kilobytes (K).

Get the best tech tips delivered straight to your inbox.

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