#! /bin/bash # # Usage: mytree [ dir ... ] # # prints statistics about a directory tree # # Dr. Harald von Fellenberg 06.01.04 22.01.04 26.01.04 # Version 10 # Achtung: dieses Script arbeitet zwingend mit bash!! DIRS=0; REG=0; LINKS=0; PIPES=0; SOCKETS=0; CDEV=0; BDEV=0; OTHERS=0; TOTAL=0 R=0; D=0; L=0; P=0; S=0; B=0; C=0; OTH=0; TOT=0; HASDATA= ; DIRNAME= ; function doit() { echo " Dirs Reg Lnks Pipes Socks Cdev Bdev Other Total Directory" echo "-----------------------------------------------------------------------" while read LINE # liest jede Zeile von ls -lAR und gibt sie parseit() do parseit $LINE done [ -n "$HASDATA" ] && printdata $D $R $L $P $S $C $B $OTH $TOT $DIRNAME echo "-----------------------------------------------------------------------"; printf "%6d%6d%6d%6d%6d%5d%5d%6d%6d %s\n" $DIRS $REG $LINKS \ $PIPES $SOCKETS $CDEV $BDEV $OTHERS $TOTAL total echo "-----------------------------------------------------------------------" return } function printdata() { # druckt eine Zeile formattiert aus printf "%6d%6d%6d%6d%6d%5d%5d%6d%6d " $1 $2 $3 \ $4 $5 $6 $7 $8 $9 shift 9 # $10 ist in sh nicht gültig ... printf "%s\n" $1 ((REG += R)) # Buchhaltung der globalen Zähler ((DIRS += D)) ((LINKS += L)) ((PIPES += P)) ((SOCKETS += S)) ((CDEV += C)) ((BDEV += B)) ((OTHERS += OTH)) ((TOTAL += TOT)) return } # function parseit() { # analyze one line of ls -lAR if [ $# -eq 1 ] && echo $1 | grep -q ':$' # ein neues Directory beginnt then DIRNAME=`echo $1 | sed 's/:$//'` # : am Ende löschen HASDATA="y" elif [ $# -gt 2 ] then MODEBITS=$1 case $MODEBITS in # Buchhaltung der verschiedenen Dateitypen -*) ((R++));; d*) ((D++));; l*) ((L++));; p*) ((P++));; s*) ((S++));; b*) ((B++));; c*) ((C++));; *) ((OTH++));; esac ((TOT++)) if [ -z "$HASDATA" ] # cmd line params are not directories then case $MODEBITS in # Filename is last except for symlinks l*) ((SHIFT=$# - 3));; *) ((SHIFT=$# - 1));; esac shift $SHIFT printdata $D $R $L $P $S $C $B $OTH $TOT $1 R=0; D=0; L=0; P=0; S=0; B=0; C=0; OTH=0; TOT=0; fi elif [ $# -eq 0 ] # Leerzeile am Ende eines Directory then printdata $D $R $L $P $S $C $B $OTH $TOT $DIRNAME R=0; D=0; L=0; P=0; S=0; B=0; C=0; OTH=0; TOT=0; fi return } # Die Funktion doit arbeitet den Output von ls -lAR ab ls -lAR $* | doit exit 0