Last active
February 11, 2019 22:15
-
-
Save PandaFoss/459dff0c7a86f84a115102de17bb8172 to your computer and use it in GitHub Desktop.
[ES] Script que lista los archivos de un determinado directorio y permite sumar sus tamaños.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#################################### | |
# Total sizes of selected files # | |
# ______________by_________________# | |
# Max Ferrer <maxi.fg13@gmail.com> # | |
#################################### | |
# Publicado bajo Licencia GPL de GNU (https://www.gnu.org/licenses/gpl.html) | |
# Variables | |
declare -a SELECCION; | |
BOLD="\e[1m" | |
UNDERLINED="\e[4m" | |
NORMAL="\e[0m" | |
RED="\e[31m" | |
YELLOW="\e[33m" | |
BLUE="\e[34m" | |
# Verificamos que no hayan argumentos innecesarios | |
if [ "$2" ] | |
then | |
echo -e "${YELLOW}${BOLD}\nDemasiados argumentos.\n${NORMAL}"; | |
echo -e "${UNDERLINED}Ayuda${NORMAL}:"; | |
echo -e "\t"$0" <directorio/a/evaluar/>\n"; | |
echo -e "Saliendo...\n"; | |
exit | |
fi | |
# Creamos el arreglo "LISTA" que almacena los archivos del directorio que le indiquemos (por defecto el actual) | |
if [ -z "$1" ] | |
then | |
declare -a LISTA=($(ls -pAL | grep -v /)) | |
# Chequeamos si el directorio está vacío | |
if [ -z "$(ls -pAL | grep -v /)" ] | |
then | |
echo -e "${YELLOW}${BOLD}\nEl directorio no posée archivos.\n${NORMAL}"; | |
exit | |
fi | |
else | |
declare -a LISTA=($(ls -pAL "$1" | grep -v /)) | |
# Chequeamos si el directorio está vacío | |
if [ -z "$(ls -pAL "$1" | grep -v /)" ] | |
then | |
echo -e "${YELLOW}${BOLD}\nEl directorio no posée archivos.\n${NORMAL}"; | |
exit | |
fi | |
fi | |
# Imprimir y enumerar archivos disponibles | |
for (( i=1; i<${#LISTA[@]}; i++ )); | |
do | |
echo -e "${BOLD}[$i]${NORMAL} ${LISTA[$i]}\n"; | |
done | |
# Seleccionar archivos | |
echo -e "${BOLD}Ingrese los numeros de los archivos que desee sumar separados por un espacio: ${NORMAL}"; | |
read -a SELECCION -p "$ " | |
# Verificar respuesta correcta | |
for (( i=0; i<${#SELECCION[@]}; i++ )); | |
do | |
if [ ${SELECCION[$i]} -gt ${#LISTA[@]} ] | |
then | |
echo -e "${RED}${BOLD}\n¡ERROR! Debes poner los numeros de los archivos que quieras evaluar separados por un espacio. Chequealo y vuelve a intentarlo."; | |
exit | |
fi | |
done | |
# Sumar y mostrar peso total | |
for (( i=0; i<${#SELECCION[@]}; i++ )); | |
do | |
TEMP=$(du -c -B1 ${LISTA[${SELECCION[$i]}]} | grep total | cut -f1) | |
let "TOTAL=TEMP+TOTAL"; | |
done | |
FIN=$(numfmt --to=iec-i ${TOTAL}) | |
echo -e "\t\t${BLUE}${BOLD}╔========================╗${NORMAL}"; | |
echo -e "\t\t${BLUE}${BOLD} TAMAÑO TOTAL: ${FIN} \t\t${NORMAL}"; | |
echo -e "\t\t${BLUE}${BOLD}╚========================╝${NORMAL}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment