When you are managing a Linux server, identifying exactly how much storage space is being consumed by specific folders is critical for maintaining system health. In a graphical desktop, you can simply right-click a folder and view its properties. However, when you are managing a headless Ubuntu server via an SSH terminal, you must rely on command-line utilities to calculate folder sizes.
The standard ls command is excellent for listing files, but if you use it on a directory, it only shows the tiny size of the directory metadata itself (usually 4KB), not the combined weight of the files inside it. To find the true, cumulative size of a directory, you must use the Disk Usage (du) command.
The Basic ‘du’ Command
The du command calculates the storage space consumed by a directory and all of its subdirectories.
- Open your Ubuntu terminal.
- Type
dufollowed by the path of the directory you want to check, and press Enter.
du /var/log
By default, this command is overwhelming. It will print a massive, scrolling list of every single sub-folder inside /var/log, and display their sizes in raw kilobytes. This makes it very difficult to read.
How to Make the Output Readable (Human Readable Flag)
To make the output useful, you need to append two specific flags to the command: -s (summarize) and -h (human-readable).
- -s (Summarize): Tells the command to suppress the long list of subdirectories and only output one single total number for the main folder you targeted.
- -h (Human-readable): Tells the command to automatically convert confusing kilobyte numbers into easily readable Megabytes (M) or Gigabytes (G).
Run the command like this:
du -sh /var/log
The terminal will output a clean, single line, such as: 4.2G /var/log. This instantly tells you that the entire log directory contains 4.2 Gigabytes of data.
How to Check the Size of All Folders in Your Current Location
If your hard drive is full, but you do not know which specific folder is causing the problem, you can ask the du command to summarize every folder sitting in your current location.
- Navigate to the directory you suspect is bloated (for example, your home directory).
- Run the following command:
du -sh *
The asterisk (*) acts as a wildcard. The terminal will scan everything in your current location and output a neat list, summarizing the total size of each top-level folder individually. (Note: Depending on how many files are inside those folders, this command may take several seconds or even a few minutes to finish calculating before it prints the results to the screen).
Security Note: If you are trying to calculate the size of system directories like /var, /etc, or /root, the standard user account does not have permission to read the files inside them. The command will output “Permission denied” errors, and the final calculation will be inaccurate. You must prepend sudo (e.g., sudo du -sh /var) to get the true total.