How to check disk usage in Linux

How to Check Disk Usage in Linux Monitoring disk usage is a fundamental system administration task in Linux environments. Whether you're managing a personal server, maintaining enterprise systems, or troubleshooting performance issues, understanding how to check disk usage effectively can prevent storage-related problems and optimize system performance. This comprehensive guide covers various methods and tools for checking disk usage in Linux, from basic command-line utilities to advanced monitoring solutions. You'll learn practical techniques that work across different Linux distributions and scenarios. Why Monitor Disk Usage? Before diving into the technical details, it's important to understand why disk usage monitoring matters: - Prevent system crashes: Running out of disk space can cause applications to fail and systems to become unstable - Performance optimization: Full disks can significantly slow down system performance - Capacity planning: Understanding usage patterns helps plan for future storage needs - Security monitoring: Unusual disk usage patterns may indicate security issues or malware - Cost management: Efficient storage utilization reduces hardware and cloud storage costs Basic Disk Usage Commands The `df` Command - File System Overview The `df` (disk free) command provides a high-level overview of file system disk usage. It's the most commonly used tool for checking available disk space. Basic `df` Usage ```bash df ``` This displays output similar to: ``` Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 20511312 4823424 14636560 25% / /dev/sda2 51200000 2048000 46080000 5% /home tmpfs 1024000 12000 1012000 2% /tmp ``` Useful `df` Options Human-readable format: ```bash df -h ``` Output: ``` Filesystem Size Used Avail Use% Mounted on /dev/sda1 20G 4.7G 14G 25% / /dev/sda2 49G 2.0G 44G 5% /home tmpfs 1.0G 12M 1.0G 2% /tmp ``` Display specific file system types: ```bash df -t ext4 ``` Exclude specific file system types: ```bash df -x tmpfs -x devtmpfs ``` Display inode information: ```bash df -i ``` The `du` Command - Directory Usage Analysis The `du` (disk usage) command analyzes disk usage for directories and files, making it perfect for identifying space-consuming content. Basic `du` Usage Check current directory: ```bash du ``` Human-readable format: ```bash du -h ``` Summarize directory usage: ```bash du -sh /path/to/directory ``` Example output: ``` 2.5G /var/log 1.2G /home/user/Documents 500M /tmp ``` Advanced `du` Examples Find the largest directories in the current location: ```bash du -h --max-depth=1 | sort -hr ``` Display only directories larger than 100MB: ```bash du -h --threshold=100M ``` Exclude specific directories: ```bash du -h --exclude="*.tmp" --exclude="/proc" ``` Check disk usage with progress indicator: ```bash du -h / | pv -l > /dev/null ``` Advanced Disk Usage Tools Using `ncdu` - Interactive Disk Usage Analyzer `ncdu` (NCurses Disk Usage) provides an interactive, user-friendly interface for analyzing disk usage. Installing `ncdu` Ubuntu/Debian: ```bash sudo apt install ncdu ``` CentOS/RHEL/Fedora: ```bash sudo yum install ncdu # CentOS 7/RHEL 7 sudo dnf install ncdu # Fedora/CentOS 8+ ``` Using `ncdu` ```bash ncdu / ``` This opens an interactive interface where you can: - Navigate directories with arrow keys - Press `Enter` to enter directories - Press `d` to delete files/directories - Press `q` to quit The `lsof` Command for Open Files Sometimes disk space issues are caused by deleted files that are still held open by processes. ```bash lsof +L1 ``` This shows files that have been deleted but are still open, consuming disk space. Using `find` for Specific File Searches Find large files (over 100MB): ```bash find / -type f -size +100M -exec ls -lh {} \; ``` Find files larger than 1GB: ```bash find / -type f -size +1G -print0 | xargs -0 ls -lh ``` Find files modified in the last 7 days: ```bash find / -type f -mtime -7 -size +10M ``` Checking Disk Usage by File Type Analyze Usage by File Extensions Find the largest log files: ```bash find /var/log -name "*.log" -type f -exec du -h {} \; | sort -hr | head -20 ``` Identify space usage by file extension: ```bash find /home -type f -name "." | sed 's/.*\.//' | sort | uniq -c | sort -nr ``` Database and Application-Specific Analysis MySQL database sizes: ```bash du -sh /var/lib/mysql/* ``` Docker container and image usage: ```bash docker system df docker system df -v # verbose output ``` Monitoring Disk Usage in Real-Time Using `watch` for Continuous Monitoring ```bash watch -n 5 df -h ``` This updates the disk usage display every 5 seconds. Creating Custom Monitoring Scripts Here's a simple bash script for disk usage alerts: ```bash #!/bin/bash disk_monitor.sh THRESHOLD=80 EMAIL="admin@example.com" df -H | awk '{ if (NR > 1 && int($5) >= '$THRESHOLD') { print "WARNING: " $1 " is " $5 " full" system("echo \"Disk " $1 " is " $5 " full\" | mail -s \"Disk Space Alert\" '$EMAIL'") } }' ``` Make it executable and add to crontab: ```bash chmod +x disk_monitor.sh crontab -e Add: /30 * /path/to/disk_monitor.sh ``` Disk Usage Analysis Strategies Systematic Approach to Disk Analysis 1. Start with `df -h` to get an overview 2. Use `du -sh /*` to identify problem directories 3. Drill down with `du -h /problematic/directory | sort -hr | head -20` 4. Investigate specific file types or applications Common Disk Space Consumers Log files: ```bash find /var/log -type f -name "*.log" -mtime +30 -size +100M ``` Temporary files: ```bash du -sh /tmp /var/tmp ``` Package manager cache: ```bash Ubuntu/Debian du -sh /var/cache/apt CentOS/RHEL du -sh /var/cache/yum ``` User home directories: ```bash du -sh /home/* | sort -hr ``` Performance Considerations Optimizing Disk Usage Commands Limit filesystem traversal: ```bash du -h --max-depth=2 /large/directory ``` Use specific mount points: ```bash df /home /var /usr ``` Background large operations: ```bash nohup du -h / > disk_usage.txt 2>&1 & ``` Network File Systems For NFS or other network filesystems: ```bash df -h -t nfs du -h --exclude="/nfs/mount" / ``` Troubleshooting Common Issues Issue: `df` and `du` Show Different Results This often occurs when files are deleted but still held open by processes. Solution: ```bash Find processes with deleted files lsof +L1 Restart services holding deleted files sudo systemctl restart service_name Or find and kill specific processes fuser /path/to/deleted/file ``` Issue: Permission Denied Errors Solution: ```bash Use sudo for system directories sudo du -sh /root /var Or redirect errors du -sh / 2>/dev/null Or save errors for review du -sh / 2>errors.log ``` Issue: Command Takes Too Long Solutions: 1. Limit depth: ```bash du -h --max-depth=1 / ``` 2. Use timeout: ```bash timeout 300 du -sh /large/directory ``` 3. Background processing: ```bash du -sh /large/directory > output.txt & ``` Automated Disk Usage Reports Creating Comprehensive Reports ```bash #!/bin/bash disk_report.sh echo "=== Disk Usage Report $(date) ===" echo echo "=== File System Overview ===" df -h echo echo "=== Largest Directories ===" du -h / 2>/dev/null | sort -hr | head -20 echo echo "=== Largest Files ===" find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | head -10 ``` Integration with System Monitoring Most monitoring solutions like Nagios, Zabbix, or Prometheus can integrate these commands for automated alerting. Example Nagios check: ```bash #!/bin/bash DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//') if [ $DISK_USAGE -gt 90 ]; then echo "CRITICAL - Disk usage is ${DISK_USAGE}%" exit 2 elif [ $DISK_USAGE -gt 80 ]; then echo "WARNING - Disk usage is ${DISK_USAGE}%" exit 1 else echo "OK - Disk usage is ${DISK_USAGE}%" exit 0 fi ``` Best Practices for Disk Usage Management Regular Monitoring Routine 1. Daily: Quick `df -h` check 2. Weekly: Detailed directory analysis with `du` 3. Monthly: Comprehensive cleanup and capacity planning 4. Quarterly: Review growth trends and adjust monitoring thresholds Preventive Measures - Log rotation: Configure proper log rotation - Temporary file cleanup: Regular cleanup of `/tmp` directories - Package cache management: Clean package manager caches regularly - Automated alerts: Set up proactive monitoring with appropriate thresholds Documentation and Change Management Keep records of: - Disk usage trends - Major cleanups performed - Storage expansion decisions - Application-specific usage patterns Conclusion Effective disk usage monitoring in Linux requires a combination of tools, techniques, and consistent practices. The commands and methods covered in this guide provide a comprehensive foundation for managing disk space across various Linux environments. Start with basic commands like `df` and `du` for routine monitoring, then incorporate advanced tools like `ncdu` and custom scripts for deeper analysis. Remember that proactive monitoring and automated alerting can prevent many disk space-related issues before they impact system performance. Regular practice with these tools and techniques will make disk usage monitoring a natural part of your Linux system administration workflow. Whether you're managing a single server or a complex infrastructure, these skills are essential for maintaining healthy, performant systems. By implementing the strategies and tools discussed in this guide, you'll be well-equipped to handle disk usage challenges and maintain optimal system performance in your Linux environment.