VPS Hosting

VPS Hosting

Buy Now

How to check the size of a directory in Linux

Efficient storage management is critical in Linux, whether you’re running a personal server or managing enterprise infrastructure. Monitoring directory size and disk usage helps prevent performance bottlenecks, optimise resource allocation, and detect early signs of issues like malware or runaway log files.

This guide shows you how to monitor directory size using the du command and track overall disk usage with df, helping you maintain a responsive and secure system.

Monitor Directory Size with the du Command

The du (disk usage) command is ideal for checking the size of directories and subdirectories. It offers flexible options for different reporting formats and depth levels.

Check Directory Size

To view the size of a specific directory and its subdirectories in kilobytes:

du /path/to/directory

Use Human-Readable Format

For easier interpretation of sizes in KB, MB, or GB:

du -h /path/to/directory

Limit Depth of Output

To avoid overly detailed results, restrict how deep du scans:

du -h --max-depth=1 /path/to/directory

This is especially useful when scanning high-level directories like /home or /var.

Get a Summary Only

To display just the total size of a directory:

du -sh /path/to/directory

Identify Large Directories

To locate the largest directories within a location:

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

This sorts the output in descending order by size, making it easy to pinpoint storage-heavy areas.

Monitor Disk Usage with the df Command

The df (disk free) command provides insights into disk space usage across all mounted file systems.

Check Inode Usage

Inodes store metadata for files. If your system runs out of inodes, new files can’t be created even if space is available:

df -i

View All Mounted File Systems

To see disk usage across all partitions:

df

This includes total size, space used, available space, and the usage percentage.

Human-Readable Format

To display results in a clearer format:

df -h

Check a Specific File System

Target a particular partition or mount point for more focused monitoring:

df -h /dev/sda2

Display File System Types

To identify the format of each mounted file system (e.g., ext4, xfs, tmpfs):

df -T