Skip to content

Instantly share code, notes, and snippets.

@RyanZurrin
Created May 13, 2024 11:33
Show Gist options
  • Save RyanZurrin/05490fcc9df7d443324024ea0fd9286b to your computer and use it in GitHub Desktop.
Save RyanZurrin/05490fcc9df7d443324024ea0fd9286b to your computer and use it in GitHub Desktop.
bash script to generate space usage reports
#!/bin/bash
# Directory to scan; you can modify this to be the root '/' or any specific directory
TARGET_DIRECTORY="/"
# Output directory for the reports
REPORT_DIR="/var/log/space_reports"
mkdir -p "${REPORT_DIR}"
# Filename includes date for weekly tracking
REPORT_FILE="${REPORT_DIR}/space_report_$(date +%Y-%m-%d).txt"
# Generating the space usage report
echo "Generating space usage report for ${TARGET_DIRECTORY} on $(date)" > "${REPORT_FILE}"
echo "=========================================" >> "${REPORT_FILE}"
# List the top 10 directories by space usage
du -h "${TARGET_DIRECTORY}" --max-depth=1 2>/dev/null | sort -rh | head -n 10 >> "${REPORT_FILE}"
echo "Report generated at ${REPORT_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment