- Posted on
- • Administration
Monitoring Disk Usage with Bash Commands
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Monitoring disk usage is essential for maintaining system health and ensuring adequate storage space. Here’s how you can monitor disk usage using various Bash commands:
1. Check Disk Space Usage
Command:
df
Usage:
- View disk usage for all mounted filesystems:
df -h
-h
: Displays output in human-readable format (e.g., GB, MB).Filter for a specific filesystem or directory:
df -h /path/to/directory
2. Analyze Directory Sizes
Command:
du
Usage:
- Display the size of a directory and its subdirectories:
du -h /path/to/directory
- Show only the total size of a directory:
du -sh /path/to/directory
-s
: Summarize the total size.-h
: Human-readable format.
3. Monitor Disk Space in Real-Time
Command:
watch
Usage:
- Use
watch
to rundf
repeatedly at intervals:
watch -n 5 df -h
-n 5
: Refresh every 5 seconds.
- Use
4. Find Large Files
Command:
find
Usage:
- Search for files larger than 1 GB:
find /path/to/search -type f -size +1G
- List large files in human-readable format:
find /path/to/search -type f -size +1G -exec ls -lh {} \;
5. Monitor Inode Usage
Command:
df
(with the-i
option)Usage:
- Check inode usage to ensure you’re not running out:
df -i
6. Automate Monitoring with a Script
Example Bash Script:
#!/bin/bash # Define thresholds THRESHOLD=80 ALERT_EMAIL="admin@example.com" # Monitor disk space df -h | awk 'NR>1 {if ($5+0 > '"$THRESHOLD"') print $0}' > /tmp/disk_alert.txt # Send email alert if thresholds are exceeded if [ -s /tmp/disk_alert.txt ]; then mail -s "Disk Space Alert" "$ALERT_EMAIL" < /tmp/disk_alert.txt fi
7. System-Wide Disk Monitoring Tools
Consider using tools like ncdu
or iotop
for more advanced monitoring:
ncdu: A fast, interactive disk usage analyzer.
sudo apt install ncdu # For Debian-based systems sudo dnf install ncdu # For Fedora/RHEL-based systems sudo zypper install ncdu # For openSUSE ncdu /path/to/analyze
iotop: Monitor real-time disk I/O usage.
sudo apt install iotop # For Debian-based systems sudo dnf install iotop # For Fedora/RHEL-based systems sudo zypper install iotop # For openSUSE iotop
These commands and tools provide both basic and advanced methods for monitoring disk usage, helping you maintain system performance and prevent storage issues.
Further Reading
To delve deeper into the topic of monitoring disk usage with Bash commands and related activities, you might consider exploring additional resources that cover advanced scripting, performance tuning, storage management, and real-time system monitoring. Below are a few recommendations:
Advanced Bash-Scripting Guide by Mendel Cooper:
- A comprehensive guide for scripting including filesystem monitoring, which can help expand the knowledge on Bash.
- URL: https://tldp.org/LDP/abs/html/
Linux Performance:
- A site with detailed articles, tutorials, and resources specifically focused on Linux performance issues, including disk monitoring.
- URL: https://www.linuxperformance.org/
The Geek Diary - HowTos & Tutorials:
- Offers tutorials on Linux system administration and configuration with a focus on performance monitoring tools including
iotop
andncdu
. - URL: https://www.thegeekdiary.com/
- Offers tutorials on Linux system administration and configuration with a focus on performance monitoring tools including
DigitalOcean Community Tutorials:
- DigitalOcean provides a wealth of tutorials on managing Linux systems, including how to use commands and scripts for monitoring disk usage.
- URL: https://www.digitalocean.com/community/tutorials
By exploring these additional resources, you can further your understanding of disk usage monitoring and gain more insights into the effective management of system resources using Bash commands. This knowledge is invaluable for system administrators looking to enhance performance and manage systems more efficiently.