Monitoring Disk Usage in Linux



Disk space is a precious resource on any Linux system. Whether you're running a small home server or a large enterprise cluster, it's crucial to monitor disk usage to prevent running out of space, identify potential issues, and optimize storage allocation.

This comprehensive tutorial will explore various command-line tools and techniques for effectively monitoring disk usage on Linux, empowering you to keep your storage under control.

Command-Line Tools for Monitoring Disk Usage

Linux provides a rich set of command-line tools for monitoring disk space. Here are some of the most essential ones ?

df (Disk Free)

The df command provides a summary of disk space usage for all mounted filesystems.

  • df ? Displays disk space usage in kilobytes.
  • df -h (Human-readable) ? Displays disk space usage in a more human-readable format (e.g., KB, MB, GB). And This is the most commonly used option.
  • df -i ? Displays inode usage (inodes are data structures that store information about files).
  • df -T ? Displays the filesystem type.
  • df -a ? Shows all filesystems, including pseudo filesystems.

Example

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           2.1G     0  2.1G   0% /dev/shm
tmpfs           832M  8.6M  823M   2% /run
/dev/sda1        20G  3.4G   17G  17% /
tmpfs           416M  4.0K  416M   1% /run/user/0

du (Disk Usage)

The du command displays disk space usage for files and directories.

  • du ? Displays disk space usage in kilobytes.
  • du -h (Human-readable) ? Displays disk space usage in a more human-readable format.
  • du -s (Summary) ? Displays a summary of disk space usage for a directory.
  • du -a (All) ? Displays disk space usage for all files and subdirectories.
  • du -c (Total) ? Displays a total for all the arguments.
  • du -sh * ? Combines human-readable output and summary for each directory and file in the current directory.
  • du -h --max-depth=1 ? Shows disk usage of immediate subdirectories and files within the current directory. It's Useful for finding the largest directories quickly.

Example

du -sch /home/user1/file.bin
4.0G	/home/user1/file.bin
4.0G	total

ncdu (Ncurses Disk Usage)

ncdu is an interactive disk usage viewer that uses the ncurses library. It provides a more user-friendly way to navigate and analyze disk usage.

ncdu may need to be installed ?

Ubuntu 24.04 and above ?

sudo apt update
sudo apt install ncdu

RHEL, Alma Linux & Rocky Linux ?

ncdu is typically available in the Extra Packages for Enterprise Linux (EPEL) repository. If you don't have EPEL enabled, you'll need to enable it first. Here's how ?

For RHEL 9 run the following two commands ?

subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

And For CentOS Stream, Alma Linux & Rocky Linux, run the following ?

dnf config-manager --set-enabled crb
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

Now Run the following command to install ncdu ?

Once installed, simply run ncdu in the directory you want to analyze. Use the arrow keys to navigate, q to quit.

For Example, If You want to analyze the /home directory, you can simply run the following ?

lsof (List Open Files)

While not specifically for disk usage, lsof can be useful for identifying which processes are using specific files or directories. This can be helpful for troubleshooting issues where a file or directory appears to be in use.

  • lsof /path/to/file ? Shows which processes have the specified file open.
  • lsof /path/to/directory ? Shows which processes have files open within the specified directory.

Example

lsof /var/log/nginx/error.log

fdisk or lsblk (List Block Devices)

These commands are used to list block devices (hard drives, partitions). While not directly related to usage, they're essential for identifying the devices you're monitoring with df and du.

  • fdisk -l ? Lists partitions on block devices.
  • lsblk ? Lists block devices in a tree-like format, making it easier to see the relationships between devices and partitions. lsblk -f includes filesystem information.

Example

lsblk -f
NAME        FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda                                                                                
??sda1                                                                             
??sda2      ntfs               96BCC4D6BCC4B1D3                                    
??sda3      ext4   1.0         6efe3e59-336f-4088-af07-873008f52fce  209.7G    52% /home/ahmed/NewDrive
nvme0n1                                                                            
??nvme0n1p1 vfat   FAT32       01CF-36D3                             466.4M     9% /boot/efi
??nvme0n1p2 xfs                77326cab-3dfb-4ea1-adaf-aac131bb0532  115.9G    51% /var/lib/containers/storage/overlay

Analyzing Disk Usage Patterns

Monitoring disk usage is not just about checking the current state. It's also important to analyze trends over time. You can use tools like ?

  • sar (System Activity Reporter) ? sar collects system activity data, including disk I/O statistics, which can be used to track disk usage trends.
  • Monitoring tools (e.g., Nagios, Zabbix) ? These tools can collect and visualize disk usage data over time, allowing you to identify trends and potential problems.

Why Monitor Disk Usage?

Regularly monitoring disk usage is essential for several reasons ?

  • Preventing Disk Full Errors ? Running out of disk space can lead to system instability, application crashes, and data loss. Proactive monitoring allows you to take action before it's too late.
  • Identifying Storage Entries ? Monitoring helps you identify which files or directories are consuming the most disk space, allowing you to clean up unnecessary data or reallocate storage.
  • Capacity Planning ? Tracking disk usage trends over time helps you forecast future storage needs and plan for upgrades or expansion.
  • Performance Optimization ? Disk I/O performance can be affected by disk space utilization. Monitoring helps you ensure that your disks are not overly full, which can impact performance.
  • Security Auditing ? Monitoring can help identify unusual disk usage patterns that might indicate malicious activity.

Practical Use Cases

Let us now highlight some of the practical use-cases of monitoring disk usage on Linux ?

  • Checking for low disk space ? Use df -h to quickly check the available space on your filesystems.
  • Identifying large files ? Use du -a sorted by size to find the largest files on your system.
  • Cleaning up old logs ? Regularly delete or archive old log files to free up disk space.
  • Monitoring disk usage on a server ? Use monitoring tools to track disk usage trends and receive alerts when disk space is low.
  • Troubleshooting full disk errors ? Use lsof to identify which processes are using files on a full filesystem.

Example: Finding the largest files in a directory ?

du -a /path/to/directory | sort -rh | head -n 20

This will list the 20 largest files and directories within /path/to/directory in human-readable format.

Conclusion

Monitoring disk usage is a critical task for any Linux user or system administrator. By using the command-line tools and techniques described in this guide, you can effectively keep track of your disk space, prevent problems, and optimize storage utilization.

Regular monitoring, combined with proactive cleanup and capacity planning, will ensure that your GNU/Linux systems have the disk space they need to operate smoothly and efficiently.

Updated on: 2025-03-26T15:35:02+05:30

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements