As a Linux developer, having a deep understanding of how to check directory sizes efficiently is crucial. Whether optimizing storage on a server or debugging performance issues in an application, knowing the exact disk space used by specific directories gives invaluable insight.

In this comprehensive guide, we will cover several powerful terminal commands to check directory size in Linux, along with detailed examples and explanations of the output.

Prerequisites

This guide assumes you have basic knowledge of the Linux terminal, including:

  • Navigating the file system
  • Using sudo to run commands as root
  • Installing packages with a package manager like apt

The examples use Ubuntu 20.04, but most commands should work across all major Linux distributions.

du – Estimate File Space Usage

The du (disk usage) command is the go-to tool for checking directory sizes in Linux. Here is the basic syntax:

du [options] [directory]

Running du by itself will show disk usage for the current working directory:

du
12      ./documents
8       ./pictures
4       ./downloads
24      .

The first column shows the total size in kilobytes. We can append the -h flag to display human-readable file sizes:

du -h
12K     ./documents  
8.0K    ./pictures
4.0K    ./downloads
24K     .

Much better! Now let‘s total up everything using -c:

du -ch 
12K     ./documents
8.0K    ./pictures  
4.0K    ./downloads
24K     .

We can also specify a particular directory to check. This will include subdirectories by default:

sudo du -ch /var
1.3M    /var/lib
152K    /var/log
176K    /var/cache
1.6M    /var

The -h shows the size in the most appropriate unit (MB, GB etc).

Limit depth with --max-depth

du will recursively check the full subdirectory structure by default. We can limit this using --max-depth.

For example, to only check one level down in /var:

sudo du -ch --max-depth=1 /var  
1.3M    /var/lib
152K    /var/log
176K    /var/cache
4.0K    /var/spool
4.0K    /var/opt
8.0K    /var/backups 
1.6M    /var

Now only the main subdirectories in /var are measured, without going deeper.

Quick directory overview with tree

The tree command prints a visual tree of the subdirectories and files within a directory. Appending -h shows the size for folders:

tree -h
.
├── [4.0K]  documents
├── [8.0K]  pictures
└── [12K] downloads

We can check a specific directory by passing it as the argument:

tree -h /var
/var
├── [1.3M] lib
├── [152K] log
├── [176K] cache
├── [4.0K] spool
├── [4.0K] opt  
└── [8.0K] backups

This gives a nice quick overview of subdirectory sizes.

Interactive terminal UI with ncdu

For digging deeper into disk usage across subdirectories, ncdu provides an ncurses terminal UI.

First, install it on Ubuntu/Debian using:

sudo apt install ncdu

Simply running ncdu opens the interactive viewer on the current directory. Navigate into subdirectories using arrow keys:

ncdu 
ncdu 1.16 ~ Use the arrow keys to navigate, press ? for help                  
--- /home/john ---------------------------------------------------------------
     .config                                    92.0 KiB      
     .local                                     108.0 KiB     
     .cache                                     36.0 KiB      
     documents                                  12.0 KiB      
     downloads                                  4.0 KiB       
     pictures                                   8.0 KiB  

The interface shows sizes and allows drilling down into specific large directories. Press q to quit.

Pass a directory like ncdu /var to go directly to a specific location. The terminal UI makes comparison and exploration much easier.

Finding large files with find

Sometimes large files inside directories are taking up significant space. We can use find to locate all files over a certain size threshold.

For example, to find all files larger than 100 MB in /home:

sudo find /home -type f -size +100M
/home/john/bigfile.sql
/home/anne/large_archive.zip

The -type f flag matches only files and avoids including subdirectory sizes. Sorting by size can be useful too:

sudo find /var -type f -print0 | xargs -0 du -h | sort -rh | head -10

This finds the 10 largest files in /var and sorts them from largest to smallest.

Real-time monitoring with glances

If you need real-time visibility into directory sizes across your whole system, check out the glances terminal tool.

Install with apt then run glances to open the interactive viewer. It shows live stats on memory, disk, network, CPU and process usage. Navigate into the File System tab to view directory disk usage.

Glances makes it easy to spot directories growing rapidly over time or consuming excess disk capacity. It‘s a valuable addition to any Linux developer‘s toolkit for optimization and debugging tasks.

Closing Thoughts

I hope this guide gives you new power for tackling directory sizing challenges in Linux. Mastering tools like du, ncdu and find pays dividends across server configurations, application troubleshooting and storage optimization projects.

Let me know in the comments if you have any other useful directory analysis tips in Linux!

Similar Posts