Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Monitoring Directory Sizes on Linux with `du` and `sort`

1 min read .
Monitoring Directory Sizes on Linux with `du` and `sort`

When a Linux server starts running low on disk space, one of the fastest ways to investigate is to find which directories consume the most storage. The du and sort commands work well together for this task.

1. du: Measure Disk Usage

du reports how much disk space files and directories use. Two useful options are:

  • -s — show one summary total for each argument instead of every nested item.
  • -h — display sizes in a human-readable format such as KB, MB, or GB.

For example:

du -sh /var/*

This reports the size of each entry directly under /var.

2. sort: Order the Results by Size

Because du -h produces values with units such as M and G, use sort -h rather than numeric sort -n:

du -sh /var/* 2>/dev/null | sort -h

The 2>/dev/null part hides permission errors for directories the current user cannot read. Remove it when you want to inspect those errors.

To show the largest entries first, add -r:

du -sh /var/* 2>/dev/null | sort -hr

3. Example Output

1.1G    /var/lib
2.2G    /var/log
3.0G    /var/cache

This makes it easy to identify directories worth investigating further.

4. Check One Level Deeper

Once you find a large directory, repeat the same command inside it. For example:

du -sh /var/log/* 2>/dev/null | sort -hr

For a broader overview limited to one directory level, GNU du also supports:

du -h --max-depth=1 /var 2>/dev/null | sort -hr

Conclusion

Combining du with human-readable sorting is a simple and reliable way to locate large directories on Linux. Start with du -sh, sort with sort -h or sort -hr, then drill into the largest paths until you find the files responsible for the disk usage.

Related Posts

chevron-up