Top Linux Commands for Disk Space Monitoring

#Linux#Monitoring#System Administration#Ubuntu

by Maxime Decooman

Monitoring disk space on Linux servers is a critical aspect of system administration. Running out of disk space can cause application failures, prevent logging, and even crash your system. Here are a few commands to help you.

1. Display Free Disk Space

The most commonly used command for checking disk space. This command shows each mounted filesystem, its total size, used space, available space, usage percentage, and mount point. Regular monitoring helps prevent disk space emergencies.

df -h
# The -h flag displays sizes in human-readable format (KB, MB, GB) 
# Result example:
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            97M  1.1M   96M   2% /run
/dev/vda1        24G  8.8G   15G  38% /
tmpfs           481M  1.1M  480M   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/vda16      881M  112M  707M  14% /boot
/dev/vda15      105M  6.1M   99M   6% /boot/efi
tmpfs            97M   12K   97M   1% /run/user/1000

2. Display Filesystem Types

Add the parameter -T to get the filesystem type. Different filesystem types (ext4, xfs, btrfs, etc.) have different features and limitations regarding maximum file sizes, total capacity, and performance characteristics.

df -hT
# Result example:
Filesystem     Type   Size  Used Avail Use% Mounted on
tmpfs          tmpfs   97M  1.1M   96M   2% /run
/dev/vda1      ext4    24G  8.8G   15G  38% /
tmpfs          tmpfs  481M  1.1M  480M   1% /dev/shm
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/vda16     ext4   881M  112M  707M  14% /boot
/dev/vda15     vfat   105M  6.1M   99M   6% /boot/efi
tmpfs          tmpfs   97M   12K   97M   1% /run/user/1000

3. Check Inode Usage

Each file on a Linux system uses an inode (index node), which stores metadata about the file. An inode exhaustion can prevent new file creation even with available disk space. Monitoring inode usage is critical for systems that handle numerous small files, such as mail or web servers.

df -i
# Result example
Filesystem      Inodes  IUsed   IFree IUse% Mounted on
tmpfs           123085    784  122301    1% /run
/dev/vda1      3145728 210820 2934908    7% /
tmpfs           123085      3  123082    1% /dev/shm
tmpfs           123085      3  123082    1% /run/lock
/dev/vda16       58496    604   57892    2% /boot
/dev/vda15           0      0       0     - /boot/efi
tmpfs            24617     32   24585    1% /run/user/1000

4. Check Size of a Directory

The disk usage (du) command with -s (summarize) and -h (human-readable) flags displays the total size of a specified directory and its contents.

du -sh /var/log
# Result
1.2G /var/log

This command is invaluable for quickly identifying which directories are consuming significant space. Use it to target potential cleanup locations when space is running low.

5. Display Directory Sizes

A more granular alternative to -sh is to show the size of all immediate subdirectories without recursing deeper into the directory tree. This helps identify which top-level directories are consuming the most space. You may need to use sudo to have the read permissions.

 sudo du -h --max-depth=1 /var
# The --max-depth=1 parameter is particularly useful for exploring space
# usage hierarchically without being overwhelmed by thousands of files.
# Result
4.0K  /var/mail
40M   /var/spool
4.0K  /var/local
1.3G  /var/log
...
2.7G  /var

6. Sort Directories by Size

Pipe the sort for sorting to display directories ordered by size (largest first), making it easier to identify space hogs.

sudo du -h --max-depth=1 /var | sort -hr
# -h parameter compares according to human readable numbers.
# -r parameter means reverse (biggest to smallest).
# Result
1.3G  /var
1.2G  /var/log
132M  /var/cache
12M   /var/backups
24K   /var/tmp
4.0K  /var/mail

7. Interactive Disk Usage Analyzer

A more user-friendly, interactive version of du with a text-based interface. It allows navigation through directories and provides a visual representation of space usage.

ncdu /var

ncdu provides key-based navigation to browse directories, delete files, and quickly identify space-consuming directories.

# Result example
--- /var -----------------------------------------------------
.   1.3 GiB [##################] /log
. 577.7 MiB [#######           ] /lib
. 336.3 MiB [####              ] /cache
    2.9 MiB [                  ] /backups
. 420.0 KiB [                  ] /spool
   52.0 KiB [                  ] /snap

8. Find Large Files

Uses the find command to search the entire filesystem for files larger than 100MB. This targeted approach helps identify specific large files that might be candidates for deletion or archiving.

find / -type f -size +100M
# Adjust the size parameter (+100M) as needed. 
# Add `-exec ls -lh {} \;` to see detailed file information 
# including human-readable sizes.
# Result
/var/log/journal/7aa9dd79e9e94f0f9519591df3024c5a/system.journal
/home/user/Downloads/large-file.iso

9. List Files Sorted by Size

Lists files in the current directory, sorted by size (largest first) with human-readable sizes. This provides a quick snapshot of the largest files in a specific directory.

ls -laSh
# The parameters used are: 
#  -l (long format), 
#  -a (all files including hidden), 
#  -S (sort by size), and 
#  -h (human-readable sizes).
# Result example:
total 1.2G
-rw-r--r-- 1 user user 500M Apr 10 12:34 database.dump
-rw-r--r-- 1 user user 350M Apr  9 09:12 backup.tar.gz
-rw-r--r-- 1 user user 120M Apr  8 22:45 logs.tar

10. List Block Devices

This command provides an overview of your storage devices and their organization. It helps identify all available storage, including unmounted devices, which is useful for storage planning and troubleshooting.

lsblk
# Result example:
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   500G  0 disk 
├─sda1   8:1    0    30G  0 part /
├─sda2   8:2    0   434G  0 part /home
└─sda3   8:3    0    36G  0 part [SWAP]
sdb      8:16   0     1T  0 disk 
└─sdb1   8:17   0     1T  0 part /data

11. List Disk Partitions

This low-level tool shows the exact layout of partitions on the disk, which is helpful for disk management, troubleshooting, and planning partition changes. It requires root privileges.

sudo fdisk -l
# Result example:
Disk /dev/sda: 500 GiB
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048  62914559  62912512    30G 83 Linux
/dev/sda2        62914560 973078527 910163968   434G 83 Linux
/dev/sda3       973078528 1048575999  75497472    36G 82 Linux swap

12. Monitor Disk I/O

Displays real-time disk I/O statistics for processes, similar to how top shows CPU usage. This helps identify which processes are causing heavy disk activity.

sudo iotop
# Result example:
Total DISK READ:       0.00 B/s | Total DISK WRITE:       0.00 B/s
Current DISK READ:     0.00 B/s | Current DISK WRITE:     0.00 B/s
    PID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND
      1  be/4  root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --system
  1234  be/4  user        0.00 B/s    1.12 M/s  0.00 %  0.12 % python backup.py

13. Display Mounted Filesystems

This command provides a comprehensive view of all mount points in your system, which helps in understanding the filesystem hierarchy and diagnosing mount-related issues.

findmnt
# Result example:
TARGET                                SOURCE     FSTYPE     OPTIONS
/                                     /dev/sda1  ext4       rw,relatime
├─/sys                                sysfs      sysfs      rw,nosuid,nodev,noexec
│ ├─/sys/kernel/security              securityfs securityfs rw,nosuid,nodev,noexec
│ ├─/sys/fs/cgroup                    tmpfs      tmpfs      ro,nosuid,nodev,noexec
...
├─/home                               /dev/sda2  ext4       rw,relatime

14. Logical Volume Management

These commands are essential for managing LVM (Logical Volume Management)-based storage, which provides flexibility for resizing filesystems and managing storage across multiple physical disks. Many enterprise systems use LVM for storage management.

sudo pvs
# Result example:
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda3  vg_sys lvm2 a--  100.00g 10.00g
sudo vgs
# Result example:
  VG     #PV #LV #SN Attr   VSize   VFree
  vg_sys   1   2   0 wz--n- 100.00g 10.00g
sudo lvs
# Result example:
  LV      VG     Attr       LSize   Pool Origin Data%  Meta%
  lv_root vg_sys -wi-ao----  50.00g
  lv_home vg_sys -wi-ao----  40.00g

15. Check Disk Usage by File Type

The find command combined with du can help identify space usage by specific file types, which is useful for targeted cleanup operations.

sudo find /path/to/search -name "*.log" -type f -exec du -ch {} \+ | grep total$
# This example finds all .log files and shows their total size
# You can replace "*.log" with any file extension (.zip, .mp4, etc.)
# Result example:
450M    total

An article will follow to build a monitoring dashboard based on some of these commands.