From owner-freebsd-questions Thu Dec 9 7:45:21 1999 Delivered-To: freebsd-questions@freebsd.org Received: from cc942873-a.ewndsr1.nj.home.com (cc942873-a.ewndsr1.nj.home.com [24.2.89.207]) by hub.freebsd.org (Postfix) with ESMTP id 953FF155E5 for ; Thu, 9 Dec 1999 07:45:16 -0800 (PST) (envelope-from cjc@cc942873-a.ewndsr1.nj.home.com) Received: (from cjc@localhost) by cc942873-a.ewndsr1.nj.home.com (8.9.3/8.9.3) id KAA60420; Thu, 9 Dec 1999 10:49:30 -0500 (EST) (envelope-from cjc) From: "Crist J. Clark" Message-Id: <199912091549.KAA60420@cc942873-a.ewndsr1.nj.home.com> Subject: Re: hopefully three simple questions In-Reply-To: <4.2.0.58.19991208084058.009ac100@mail.enterit.com> from Jim Conner at "Dec 8, 1999 08:44:50 am" To: jconner@enterit.com (Jim Conner) Date: Thu, 9 Dec 1999 10:49:30 -0500 (EST) Cc: freebsd-questions@FreeBSD.ORG (FreeBSD Questions) Reply-To: cjclark@home.com X-Mailer: ELM [version 2.4ME+ PL54 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Jim Conner wrote, > I sent these to Crist last night. The shell script doesn't work :) (what > can I say...I threw it together in 20 minutes)....however, the find will at > least give a count of files in each directory based on where you run the > find command from and store the output to a file of your choice. I don't > think you will be able to answer this guy's questions with simple command > on the command line. Its going to require scripting (I think) > > find . -type d -exec wc -l {} \; >files.out I don't see how that would work. You are using wc(1) on the diretory, which in this context is a binary file with some info in it. It does not give the right answer, % ls -a / . boot.help kernel.GENERIC share usr2 .. c kernel.config stand usr3 .cshrc cdrom kernel.old sys usr4 .profile compat mnt tmp var COPYRIGHT dev modules u1 bin etc proc u2 boot home root users boot.config kernel sbin usr % ls -a / | wc -l 36 % wc -l / 4 / > #!/bin/ksh > > counter=0 > var=0 > file=files.out > find . -type d -exec wc -l {} \; >$file > > for loop in `cat $file | grep -E [0-9] | awk '{print $1}'` > do > counter=$((counter + 1)) > var[$counter]=$loop > if [ $counter = 2 ] > then > if [ ${var[1]} -gt ${var[2]} ] > then > largest=${var[1]} > counter=1 > elif [ ${var[2]} -gt ${var[1]} ] > then > largest=${var[2]} > counter=1 > else > counter=1 > fi > else > blah=0 > fi > done > echo "The largest directory had $largest files: `grep $largest $file`" > rm $file I think the best way to do this would be a recursive shell script, #!/bin/sh # # dirmax - cjc, 1999/12/09 # # usage: dirmax [dir] # # Returns the maximum depth and number of members in # the directory with the most members on stdout. If # no argument is give, the pwd is used. # This function takes the following args, # usage: dirstats dir depth maxdepth dirstats () { cd $1 MAXNUM=`ls -A | wc -l` MAXDEPTH=0 for DIR in `ls -A`; do if [ -d $DIR ]; then set -- `dirstats $DIR $MAXDEPTH` if [ $1 -gt $MAXDEPTH ]; then MAXDEPTH=$1 fi if [ $2 -gt $MAXNUM ]; then MAXNUM=$2 fi fi done MAXDEPTH=`expr $MAXDEPTH + 1` echo $MAXDEPTH $MAXNUM } if [ $# -gt 1 ]; then echo "$0: usage: dirmax [dir]" >&2 elif [ $# -eq 0 ]; then STARTDIR=. else STARTDIR=$1 fi dirstats $STARTDIR 0 exit 0 Fair warning: There are no checks for symbolic links making infinte loops, handling of directories without read permissions, etc. -- Crist J. Clark cjclark@home.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message