BIG...
Postat: 23 jun 2022, 12:43
Tror att jag lagt ut det här scriptet här innan, men det är hursomhelst vidareutvecklat och uppdaterat nu.
Tanken bakom scriptet är att man ska använda det för att hitta stora filer och kunna rensa på disken när det blir fullt, eller i övrigt när man har lust. Det går att köra direkt i terminalen, så det funkar även när disken är så full så att man inte kan få igång grafiken.
Hur det funkar, kan man se direkt under "usage", eller om man kör det med "-h", eller "--help"...
Tanken bakom scriptet är att man ska använda det för att hitta stora filer och kunna rensa på disken när det blir fullt, eller i övrigt när man har lust. Det går att köra direkt i terminalen, så det funkar även när disken är så full så att man inte kan få igång grafiken.
Hur det funkar, kan man se direkt under "usage", eller om man kör det med "-h", eller "--help"...
Kod: Markera allt
#! /bin/bash
#
#####################################################################
#
function usage {
cat << EOD
usage: big {-d | -f}
-d|--dir|--directory
Show size of directories listed from smallest to biggest.
-f|--file
Show biggest files (this is the default).
-h|--help
Show this.
-l NUM|--list=NUM
Number of files or directories to list. NUM is an integer value.
The default is 10.
r|--rec|--recurs|--recursive
Normally big only checks the current directory when -f is specified,
but if -r is also given, it also checks all subdirectories.
EOD
exit
}
#
#####################################################################
#
TEMP=`getopt -odfl:r --long help,dir,directory,file,list:,rec,recurs,recursive: -n $(basename $0) -- "$@"`
if [[ $? -ne 0 ]]; then
usage
fi
eval set -- "$TEMP"
DIRECTORY=false
FILE=false
LIST=false
LISTNUM=10
MAXDEPTH=1
CMDNAME=$(basename $0)
while true; do
case $1 in
-h|--help)
usage
exit
;;
-d|--dir|--directory)
DIRECTORY=true
shift
;;
-f|--file)
FILE=true
shift
;;
-l|--list)
shift
LISTNUM=$1
shift
;;
-r|--rec|--recurs|--recursive)
MAXDEPTH=99
shift
;;
--)
shift
break
;;
*)
echo "-Got a star... [$1]"
exit
;;
esac
done
#
if [[ $DIRECTORY == false && $FILE == false ]]; then
FILE=true
elif [[ $DIRECTORY == true && $FILE == true ]]; then
echo
echo "-Not possible to specify both directory and file..."
usage
exit
fi
#
#####################################################################
#
echo
if [[ $DIRECTORY == true ]]; then
du --max-depth 1 $1 | sort -n | tail -$LISTNUM | awk '{ print substr($0, index($0,$2)) }' | \
while read DIR; do
if [[ "$DIR" != "." ]]; then
du --max-depth 0 -h "${DIR}" 2> /dev/null
fi
done
# du --max-depth 0 -h .
elif [[ $FILE == true ]]; then
find . -maxdepth $MAXDEPTH -type f -exec ls -hs {} \; | sort -n | tail -$LISTNUM
fi
echo