Skip to content

Instantly share code, notes, and snippets.

@alonsoir
Last active March 6, 2025 08:33
Show Gist options
  • Save alonsoir/a5b79bb69db5064e034c6e4fce52950e to your computer and use it in GitHub Desktop.
Save alonsoir/a5b79bb69db5064e034c6e4fce52950e to your computer and use it in GitHub Desktop.

Revisions

  1. alonsoir revised this gist Mar 6, 2025. 1 changed file with 52 additions and 83 deletions.
    135 changes: 52 additions & 83 deletions disk_usage.sh
    Original file line number Diff line number Diff line change
    @@ -1,84 +1,53 @@
    #!/bin/bash

    # Colores para la salida
    RED='\033[0;31m'
    GREEN='\033[0;32m'
    BLUE='\033[0;34m'
    NC='\033[0m' # Sin color

    # Detectar el sistema operativo
    OS=$(uname -s)

    # Verificar dependencias
    for cmd in bc sort head du df stat find; do
    if ! command -v "$cmd" >/dev/null 2>&1; then
    echo -e "${RED}Error: $cmd no está instalado o no se encuentra en el PATH${NC}"
    exit 1
    fi
    done

    # Función para convertir bytes a formato legible
    human_readable() {
    local bytes="$1"
    if ! [[ "$bytes" =~ ^[0-9]+$ ]]; then
    echo "0B"
    return
    fi
    if [[ $bytes -lt 1024 ]]; then
    echo "${bytes}B"
    elif [[ $bytes -lt $((1024*1024)) ]]; then
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/1024" | bc))KB"
    elif [[ $bytes -lt $((1024*1024*1024)) ]]; then
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/(1024*1024)" | bc))MB"
    else
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/(1024*1024*1024)" | bc))GB"
    fi
    }

    echo -e "${BLUE}Analizando uso del disco en $OS...${NC}\n"

    # Mostrar espacio total, usado y disponible
    echo -e "${GREEN}Información general del disco:${NC}"
    df -h / | awk 'NR==2 {print "Total: " $2 "\nUsado: " $3 "\nDisponible: " $4 "\nPorcentaje usado: " $5}'
    echo

    # Directorios principales a analizar
    echo -e "${GREEN}Directorios que más espacio ocupan en /:${NC}"
    sudo du -d 1 -x / 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable $((size * 1024)))
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done
    echo

    # Analizar directorio home del usuario
    echo -e "${GREEN}Directorios que más espacio ocupan en $HOME:${NC}"
    du -d 1 -x "$HOME" 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable $((size * 1024)))
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    #!/bin/zsh

    # Título del script
    echo "\n============================================="
    echo " ANALIZADOR DE ESPACIO EN DISCO PARA MACOS"
    echo "=============================================\n"

    # Información general del sistema
    echo "INFORMACIÓN DEL SISTEMA:"
    echo "---------------------------------------------"
    system_profiler SPSoftwareDataType | grep "System Version" | sed 's/^ *//'
    df -h / | grep -v Filesystem | awk '{print "Espacio total: " $2 "\nEspacio usado: " $3 "\nEspacio disponible: " $4 "\nPorcentaje usado: " $5}'
    echo "---------------------------------------------\n"

    # Tamaño de carpetas principales del usuario
    echo "ANÁLISIS DE CARPETAS PRINCIPALES DEL USUARIO:"
    echo "---------------------------------------------"
    for dir in ~/Desktop ~/Documents ~/Downloads ~/Library ~/Movies ~/Music ~/Pictures; do
    if [ -d "$dir" ]; then
    du -sh "$dir" 2>/dev/null | awk '{printf "%-12s %s\n", $1, $2}'
    fi
    done

    # Buscar archivos grandes (>500MB)
    echo -e "\n${GREEN}Buscando archivos mayores a 500MB:${NC}"
    if [ "$OS" = "Darwin" ]; then
    find / -type f -size +500M 2>/dev/null | while read -r file; do
    size=$(stat -f %z "$file" 2>/dev/null)
    if [ -n "$size" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable "$size")
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    else
    find / -type f -size +500M 2>/dev/null | while read -r file; do
    size=$(stat -c %s "$file" 2>/dev/null)
    if [ -n "$size" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable "$size")
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    fi

    echo -e "\n${BLUE}Análisis completado${NC}"
    echo "---------------------------------------------\n"

    # Archivos más grandes en el directorio del usuario (>100MB)
    echo "ARCHIVOS MÁS GRANDES (>100MB) EN CARPETA PERSONAL:"
    echo "---------------------------------------------"
    find ~/ -type f -size +100M -exec du -sh {} \; 2>/dev/null | sort -hr | head -10
    echo "---------------------------------------------\n"

    # Análisis de aplicaciones
    echo "APLICACIONES MÁS GRANDES:"
    echo "---------------------------------------------"
    du -sh /Applications/* 2>/dev/null | sort -hr | head -15
    echo "---------------------------------------------\n"

    # Análisis de caché y bibliotecas
    echo "CACHÉ Y BIBLIOTECAS DEL USUARIO:"
    echo "---------------------------------------------"
    du -sh ~/Library/Caches 2>/dev/null | awk '{print "Caché: " $1 " " $2}'
    du -sh ~/Library/Application\ Support 2>/dev/null | awk '{print "Application Support: " $1 " " $2}'
    du -sh ~/Library/Containers 2>/dev/null | awk '{print "Contenedores: " $1 " " $2}'
    echo "---------------------------------------------\n"

    # Otros directorios típicamente grandes
    echo "OTRAS UBICACIONES A VERIFICAR:"
    echo "---------------------------------------------"
    du -sh /private/var/vm 2>/dev/null | awk '{print "Memoria Virtual: " $1 " " $2}'
    du -sh /Library/Caches 2>/dev/null | awk '{print "Caché del Sistema: " $1 " " $2}'
    du -sh /System/Library 2>/dev/null | awk '{print "Bibliotecas del Sistema: " $1 " " $2}'
    echo "---------------------------------------------\n"

    echo "ANÁLISIS COMPLETADO. Revisa las secciones anteriores para ubicar archivos grandes."
  2. alonsoir revised this gist Mar 5, 2025. 1 changed file with 23 additions and 13 deletions.
    36 changes: 23 additions & 13 deletions disk_usage.sh
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,21 @@ NC='\033[0m' # Sin color
    # Detectar el sistema operativo
    OS=$(uname -s)

    # Verificar dependencias
    for cmd in bc sort head du df stat find; do
    if ! command -v "$cmd" >/dev/null 2>&1; then
    echo -e "${RED}Error: $cmd no está instalado o no se encuentra en el PATH${NC}"
    exit 1
    fi
    done

    # Función para convertir bytes a formato legible
    human_readable() {
    local bytes=$1
    local bytes="$1"
    if ! [[ "$bytes" =~ ^[0-9]+$ ]]; then
    echo "0B"
    return
    fi
    if [[ $bytes -lt 1024 ]]; then
    echo "${bytes}B"
    elif [[ $bytes -lt $((1024*1024)) ]]; then
    @@ -33,8 +45,8 @@ echo
    # Directorios principales a analizar
    echo -e "${GREEN}Directorios que más espacio ocupan en /:${NC}"
    sudo du -d 1 -x / 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $((size * 1024))) # du en Linux usa KB por defecto
    if [ -n "$path" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable $((size * 1024)))
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done
    @@ -43,29 +55,27 @@ echo
    # Analizar directorio home del usuario
    echo -e "${GREEN}Directorios que más espacio ocupan en $HOME:${NC}"
    du -d 1 -x "$HOME" 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $((size * 1024))) # Ajuste para KB
    if [ -n "$path" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable $((size * 1024)))
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done

    # Buscar archivos grandes (>500MB)
    echo -e "\n${GREEN}Buscando archivos mayores a 500MB:${NC}"
    if [ "$OS" = "Darwin" ]; then
    # macOS
    find / -type f -size +500M 2>/dev/null | while read file; do
    find / -type f -size +500M 2>/dev/null | while read -r file; do
    size=$(stat -f %z "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    if [ -n "$size" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable "$size")
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    else
    # Linux
    find / -type f -size +500M 2>/dev/null | while read file; do
    find / -type f -size +500M 2>/dev/null | while read -r file; do
    size=$(stat -c %s "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    if [ -n "$size" ] && [[ "$size" =~ ^[0-9]+$ ]]; then
    hr_size=$(human_readable "$size")
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
  3. alonsoir revised this gist Mar 5, 2025. 1 changed file with 25 additions and 11 deletions.
    36 changes: 25 additions & 11 deletions disk_usage.sh
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@ GREEN='\033[0;32m'
    BLUE='\033[0;34m'
    NC='\033[0m' # Sin color

    # Detectar el sistema operativo
    OS=$(uname -s)

    # Función para convertir bytes a formato legible
    human_readable() {
    local bytes=$1
    @@ -20,7 +23,7 @@ human_readable() {
    fi
    }

    echo -e "${BLUE}Analizando uso del disco en macOS...${NC}\n"
    echo -e "${BLUE}Analizando uso del disco en $OS...${NC}\n"

    # Mostrar espacio total, usado y disponible
    echo -e "${GREEN}Información general del disco:${NC}"
    @@ -29,10 +32,9 @@ echo

    # Directorios principales a analizar
    echo -e "${GREEN}Directorios que más espacio ocupan en /:${NC}"
    # Usamos sudo para tener acceso completo y du para calcular tamaños
    sudo du -d 1 -x / 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $size)
    hr_size=$(human_readable $((size * 1024))) # du en Linux usa KB por defecto
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done
    @@ -42,19 +44,31 @@ echo
    echo -e "${GREEN}Directorios que más espacio ocupan en $HOME:${NC}"
    du -d 1 -x "$HOME" 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $size)
    hr_size=$(human_readable $((size * 1024))) # Ajuste para KB
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done

    # Buscar archivos grandes (>500MB)
    echo -e "\n${GREEN}Buscando archivos mayores a 500MB:${NC}"
    find / -type f -size +500M 2>/dev/null | while read file; do
    size=$(stat -f %z "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    if [ "$OS" = "Darwin" ]; then
    # macOS
    find / -type f -size +500M 2>/dev/null | while read file; do
    size=$(stat -f %z "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    else
    # Linux
    find / -type f -size +500M 2>/dev/null | while read file; do
    size=$(stat -c %s "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done
    fi

    echo -e "\n${BLUE}Análisis completado${NC}"
  4. alonsoir created this gist Mar 5, 2025.
    60 changes: 60 additions & 0 deletions disk_usage.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/bin/bash

    # Colores para la salida
    RED='\033[0;31m'
    GREEN='\033[0;32m'
    BLUE='\033[0;34m'
    NC='\033[0m' # Sin color

    # Función para convertir bytes a formato legible
    human_readable() {
    local bytes=$1
    if [[ $bytes -lt 1024 ]]; then
    echo "${bytes}B"
    elif [[ $bytes -lt $((1024*1024)) ]]; then
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/1024" | bc))KB"
    elif [[ $bytes -lt $((1024*1024*1024)) ]]; then
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/(1024*1024)" | bc))MB"
    else
    echo "$(printf "%.1f" $(echo "scale=2; $bytes/(1024*1024*1024)" | bc))GB"
    fi
    }

    echo -e "${BLUE}Analizando uso del disco en macOS...${NC}\n"

    # Mostrar espacio total, usado y disponible
    echo -e "${GREEN}Información general del disco:${NC}"
    df -h / | awk 'NR==2 {print "Total: " $2 "\nUsado: " $3 "\nDisponible: " $4 "\nPorcentaje usado: " $5}'
    echo

    # Directorios principales a analizar
    echo -e "${GREEN}Directorios que más espacio ocupan en /:${NC}"
    # Usamos sudo para tener acceso completo y du para calcular tamaños
    sudo du -d 1 -x / 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done
    echo

    # Analizar directorio home del usuario
    echo -e "${GREEN}Directorios que más espacio ocupan en $HOME:${NC}"
    du -d 1 -x "$HOME" 2>/dev/null | sort -nr | head -n 10 | while read size path; do
    if [ -n "$path" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $path"
    fi
    done

    # Buscar archivos grandes (>500MB)
    echo -e "\n${GREEN}Buscando archivos mayores a 500MB:${NC}"
    find / -type f -size +500M 2>/dev/null | while read file; do
    size=$(stat -f %z "$file" 2>/dev/null)
    if [ -n "$size" ]; then
    hr_size=$(human_readable $size)
    echo -e "${RED}${hr_size}${NC} - $file"
    fi
    done

    echo -e "\n${BLUE}Análisis completado${NC}"