← Back to all snippets
BASH

Monitoring Disk Usage for Web Applications

Keep track of disk space on your web server with this Bash script, identifying large directories and potential storage hogs in your application.

#!/bin/bash

WEB_ROOT="/var/www/html"

if [ ! -d "$WEB_ROOT" ]; then
  echo "Error: Web root directory '$WEB_ROOT' not found."
  exit 1
fi

echo "--- Disk Usage Report for $WEB_ROOT ---"

echo "Total disk usage for $WEB_ROOT:"
du -sh "$WEB_ROOT"

echo "
Top 10 largest directories within $WEB_ROOT:"
du -h "$WEB_ROOT" | sort -rh | head -n 10

echo "
Largest files (top 5) within $WEB_ROOT (excluding directories):"
find "$WEB_ROOT" -type f -print0 | xargs -0 du -h | sort -rh | head -n 5
How it works: This script helps web developers monitor disk space usage within their web application's root directory. It provides a summary of total usage, lists the top 10 largest subdirectories, and identifies the top 5 largest individual files. This helps in quickly identifying and managing potential storage hogs that can impact server performance.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs