#!/bin/bash
# Find Unused Files Script
# Identifies potentially unused files, old backups, logs, etc.

echo "=========================================="
echo "UNUSED FILES ANALYSIS"
echo "Generated: $(date)"
echo "=========================================="
echo ""

WEB_ROOT="/var/www/html"
DAYS_OLD=180  # Files not accessed in 6 months

echo "=== LARGE FILES (>100MB) ==="
echo ""
find "$WEB_ROOT" -type f -size +100M -exec ls -lh {} \; 2>/dev/null | awk '{print $5, $9}' | sort -h

echo ""
echo "=== OLD BACKUP FILES ==="
echo ""
find "$WEB_ROOT" -type f \( -name "*.tar.gz" -o -name "*.zip" -o -name "*backup*" -o -name "*.bak" \) -ls 2>/dev/null | head -20

echo ""
echo "=== OLD LOG FILES (>90 days) ==="
echo ""
find "$WEB_ROOT" -type f -name "*.log" -mtime +90 -ls 2>/dev/null | head -20

echo ""
echo "=== FILES NOT ACCESSED IN $DAYS_OLD DAYS ==="
echo ""
find "$WEB_ROOT" -type f -atime +$DAYS_OLD -ls 2>/dev/null | head -30

echo ""
echo "=== NODE_MODULES DIRECTORIES ==="
echo ""
find "$WEB_ROOT" -type d -name "node_modules" -exec du -sh {} \; 2>/dev/null

echo ""
echo "=== EMPTY DIRECTORIES ==="
echo ""
find "$WEB_ROOT" -type d -empty -ls 2>/dev/null | head -20

echo ""
echo "=== TEMPORARY FILES ==="
echo ""
find "$WEB_ROOT" -type f \( -name "*.tmp" -o -name "*.temp" -o -name "*~" -o -name "*.swp" \) -ls 2>/dev/null | head -20

echo ""
echo "=== CACHE DIRECTORIES ==="
echo ""
find "$WEB_ROOT" -type d \( -name "cache" -o -name "tmp" -o -name "temp" \) -exec du -sh {} \; 2>/dev/null

echo ""
echo "=== LARGE DIRECTORIES ==="
echo ""
du -sh "$WEB_ROOT"/* 2>/dev/null | sort -h | tail -10

echo ""
echo "=========================================="
echo "Analysis Complete"
echo "=========================================="
echo ""
echo "NOTE: Review these files carefully before deleting!"
echo "Create backups before removing anything."

