Tired of running out of disk space without warning? Imagine receiving automated reports highlighting storage hogs, all neatly packaged for quick action. With the magic of ncdu
and a pinch of automation, you can stay ahead of disk space issues effortlessly. Whether it’s pinpointing bloated directories or keeping your server clean, this setup will save you time, stress, and storage space. Ready to take control? Let’s get started!
To set up ncdu
for automated disk usage reports or troubleshooting, you can use a simple script with scheduling via cron. Here’s how you can do it:
Step 1: Create a Disk Usage Report Script
- Create a shell script to generate and save disk usage reports:
nano ~/disk_usage_report.sh
- Add the following script:
#!/bin/bash
# Script to generate disk usage report using ncdu
# Directory to scan (change as needed)
SCAN_DIR="/"
# Output directory for reports
REPORT_DIR="$HOME/ncdu_reports"
mkdir -p $REPORT_DIR
# Report file name with timestamp
REPORT_FILE="$REPORT_DIR/disk_usage_$(date +%Y-%m-%d_%H-%M-%S).txt"
# Run ncdu and save the report
ncdu -o "$REPORT_FILE" "$SCAN_DIR"
# Optional: Keep only the latest 5 reports
ls -tp $REPORT_DIR | grep -v '/$' | tail -n +6 | xargs -I {} rm -- "$REPORT_DIR/{}"
- Save the file and make it executable
chmod +x ~/disk_usage_report.sh
Step 2: Automate with Cron
- Open the crontab editor
crontab -e
- Add a cron job to run the script at your preferred interval (e.g., daily at midnight):
0 0 * * * ~/disk_usage_report.sh
Step 3: View or Troubleshoot Reports
To view a report, use:
ncdu -f ~/ncdu_reports/disk_usage_<timestamp>.txt
For troubleshooting specific directories, modify the SCAN_DIR
in the script to focus on problem areas like /var
, /home
, or /tmp
.
Optional: Email Notifications
If you want to email the reports:
- Install
mailutils
(Debian/Ubuntu) or equivalent:
sudo apt install mailutils
- Add this to the script after generating the report
cat "$REPORT_FILE" | mail -s "Disk Usage Report" your_email@example.com
This setup ensures you’ll always have an up-to-date view of your disk usage, making troubleshooting quick and efficient. Need further tweaks? Let me know!