In daily server management and maintenance, disk space usage is often overlooked, yet extremely critical. This is especially true on Linux servers running web services, databases, and logging systems. If the disk fills up, it can cause website crashes, database write failures, or even system freezes. Therefore, learning to check disk space usage in Linux is essential not only for operations engineers but also for developers, webmasters, and data analysts.
Common methods for checking disk space in Linux:
1. Using the df command (Disk Free)
df is the most commonly used disk usage command, used to display the file system's disk space usage.
Basic usage:
df -h
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 40G 25G 13G 66% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
- Filesystem: Disk partition or mount point
- Size: Total capacity of the partition
- Used: Used capacity
- Avail: Available space
- Use%: Used percentage
- Mounted on: Mounted directory
Common options:
df -h
: Displays in GB/MB for a more intuitive display
df -i
: Displays inode usage (an inode full can also prevent file writes)
2. Use the du command (Disk Usage)
du is used to view the size of a directory or file.
View the size of each folder in the current directory:
du -sh *
-s
: Summarizes the total size
-h
: Displays in human-readable units
Sample output:
200M logs
1.2G mysql
500M uploads
Common Usage:
du -sh /var/*
→ Check which subdirectory within the /var directory is taking up the most space.
du -sh /home/*
→ View user directory usage.
3. Combining du with sort
To quickly find the folders that take up the most space, use:
du -ah /var | sort -rh | head -n 10
The output will list the top 10 largest files and directories in the /var directory.
4. Use ls to view large files
Sometimes a large file is taking up space. You can use:
ls -lhS /var/log
The -S flag sorts by size, making it easier to find the largest file.
5. Using ncdu (recommended)
If you prefer a graphical user interface, you can install ncdu:
# Debian/Ubuntu
sudo apt install ncdu
# CentOS
sudo yum install ncdu
run:
ncdu /
It displays an interactive interface that allows you to easily view and delete large files, making it ideal for quick troubleshooting.
How can I prevent my disk from filling up?
Enable log rotation: Linux comes with logrotate, which can periodically compress and purge logs.
Set up disk usage alerts: You can write a script that uses the df command to check the disk every hour and send an email or SMS alert if the disk usage exceeds 80%.
Monitoring tools: Use operations and maintenance tools such as Zabbix, Nagios, and Prometheus to monitor disk space in real time.
Practice proper partitioning: Mount the /var/log, /home, and /tmp partitions separately to avoid single points of failure that could overwhelm the entire system.
FAQ:
1. Why do the results of the df and du commands differ?
df displays disk usage for the entire file system. du displays disk usage for directories and files. Discrepancies may arise from caches, soft links, or deleted but unreleased files.
2. How can I find deleted files that are still occupying space?
If a process is still using a deleted file, its space will not be released. You can use:
lsof | grep deleted
After finding the corresponding process, restart the service to free it up.
3. There's plenty of disk space, but I can't write to it?
This may be due to inode exhaustion. Use df -i to check inode usage.
4. How can I quickly clean up my Linux disk?
- Clean logs:
/var/log
- Clean cache:
apt-get clean
oryum clean all
- Delete unused large files: Combine
du
andfind
.
5. What monitoring solution is recommended for enterprise servers?
For small and medium-sized enterprises, ncdu and shell scripts can be used for monitoring. For large enterprises, Prometheus and Grafana or Zabbix are recommended for real-time monitoring and alerting.
In summary: Checking disk space is an essential part of Linux system maintenance. Whether you're a personal website owner or an enterprise operator, developing a regular disk check habit, coupled with a sound monitoring mechanism, can significantly reduce the risk of system crashes.