Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 9 Dec 1999 10:49:30 -0500 (EST)
From:      "Crist J. Clark" <cjc@cc942873-a.ewndsr1.nj.home.com>
To:        jconner@enterit.com (Jim Conner)
Cc:        freebsd-questions@FreeBSD.ORG (FreeBSD Questions)
Subject:   Re: hopefully three simple questions
Message-ID:  <199912091549.KAA60420@cc942873-a.ewndsr1.nj.home.com>
In-Reply-To: <4.2.0.58.19991208084058.009ac100@mail.enterit.com> from Jim Conner at "Dec 8, 1999 08:44:50 am"

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199912091549.KAA60420>