#! /bin/bash # # Usage: mytree [ dir ... ] # # prints statistics about a directory tree # # hvf 06.01.04 22.01.04 # Version 4 mit find # Version 5 prüft Aufrufparameter # Version 7 mit ls -R, bash internen Arithmetikoperationen 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= ; 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 $OLDDIR 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=$REG + $R)) # Buchhaltung der globalen Zähler ((DIRS=$DIRS + $D)) ((LINKS=$LINKS + $L)) ((PIPES=$PIPES + $P)) ((SOCKETS=$SOCKETS + $S)) ((CDEV=$CDEV + $C)) ((BDEV=$BDEV + $B)) ((OTHERS=$OTHERS + $OTH)) ((TOTAL=$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 if [ -n "$HASDATA" ] # if we have data, i.e. not very first dir then printdata $D $R $L $P $S $C $B $OTH $TOT $OLDDIR R=0; D=0; L=0; P=0; S=0; B=0; C=0; OTH=0; TOT=0; fi OLDDIR=$DIRNAME 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 fi return } # Die Funktion doit arbeitet den Output von ls -lAR ab ls -lAR $* | doit exit 0