[net.sources] Breadth first UNIX directory walk - CSH script

geof@imagen.UUCP (Geoffrey Cooper) (11/07/86)

The following program is a C shell script that walks down a
directory structure and changes the owner and group of every
file to an argument string.  The program uses breadth-first
traversal.

The particular application is not very interesting.  FIND(I)
can do the job just as easily (FIND is too slow to use on
apollo systems).  But the program might be useful in other
contexts, since it gives access to the entire sub-pathname,
rather than just the name of the file.  For example, it could
be used to implement tree-copy.

The implementation was easy, but I had to tune it to make it
work on reasonably deep and long directories.  The most obvious
shell script got a "too many arguments" error very quickly.

- Geof Cooper
  IMAGEN
-------------------CUT HERE
#!/bin/csh
set dirs=( $argv[3-$#argv] )
set num_files=0
while ( $#dirs > 0 )
    set fi=1
    set thisdir=$dirs[1]
    set files=( $thisdir/* )
    echo "Working on directory ${thisdir}"
    /etc/chown $1 $files
    chgrp $2 $files
    while ( $fi <= $#files )
        @ numfiles++
        set thisfile=$files[$fi]
        if ( -d $thisfile ) then
            set x=`ls -ld $thisfile | colrm 2`
            if ( X$x == Xl ) then
                echo "[link  ${thisfile}]"
            else
                echo "[found ${thisfile}]"
                set dirs=( $dirs $thisfile )
            endif
        endif
        @ fi++
        end
    set dirs=( $dirs[2-$#dirs] )
end
echo "${numfiles} seen"