#!/usr/bin/env bash # analyze_disk_space # ------------------ # Analiza el uso de espacio en disco: particiones, directorios más grandes, # archivos más grandes e inodos. # # USO: # source analyze_disk_space.sh # analyze_disk_space [target_dir] [mode] # # ARGUMENTOS: # target_dir Directorio a analizar (default: /) # mode Modo de análisis: partitions|top-dirs|top-files|inodes|all (default: all) analyze_disk_space() { local target_dir="${1:-/}" local mode="${2:-all}" _ads_partitions() { echo "=== Espacio en sistemas de archivos ===" df -h --output=source,fstype,size,used,avail,pcent,target 2>/dev/null \ | grep -v "tmpfs\|devtmpfs\|loop" | column -t echo "" local high_usage high_usage="$(df -h | awk 'NR>1 && $5+0 > 90 {print $6, $5}' | grep -v "tmpfs\|devtmpfs" || true)" if [[ -n "$high_usage" ]]; then echo "ADVERTENCIA: Discos con uso >90%:" echo "$high_usage" echo "" fi } _ads_top_dirs() { local dir="${1:-.}" echo "=== Top 10 carpetas más grandes en: $(realpath "$dir") ===" du -h --max-depth=1 "$dir" 2>/dev/null | sort -rh | head -11 \ | awk '{printf "%-10s %s\n", $1, $2}' echo "" } _ads_top_files() { local dir="${1:-.}" echo "=== Top 20 archivos más grandes en: $(realpath "$dir") ===" find "$dir" -type f -exec du -h {} + 2>/dev/null | sort -rh | head -20 \ | awk '{printf "%-10s %s\n", $1, $2}' echo "" } _ads_inodes() { echo "=== Inodos disponibles ===" df -i 2>/dev/null | grep -v "tmpfs\|devtmpfs\|loop" | column -t echo "" local high_inodes high_inodes="$(df -i | awk 'NR>1 && $5+0 > 90 {print $6, $5}' | grep -v "tmpfs\|devtmpfs" || true)" if [[ -n "$high_inodes" ]]; then echo "ADVERTENCIA: Sistemas de archivos con >90% de inodos usados:" echo "$high_inodes" echo "" fi } case "$mode" in partitions) _ads_partitions ;; top-dirs) _ads_top_dirs "$target_dir" ;; top-files) _ads_top_files "$target_dir" ;; inodes) _ads_inodes ;; all) _ads_partitions _ads_top_dirs "$target_dir" _ads_top_files "$target_dir" _ads_inodes ;; *) echo "analyze_disk_space: modo desconocido '${mode}'. Usa: partitions|top-dirs|top-files|inodes|all" >&2 return 1 ;; esac } # Ejecutar si se invoca directamente if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then analyze_disk_space "$@" fi