[comp.sources.misc] v03i069: A Directory Finder

kinzler@iuvax.UUCP (Stephen Kinzler) (06/30/88)

Posting-number: Volume 3, Issue 69
Submitted-by: "Stephen Kinzler" <kinzler@iuvax.UUCP>
Archive-name: finder

Ever wonder if anyone else on the system has implemented that software
package?  Ever wonder where the subdirectories relevant to something
are?

Finder is a quick way to answer these questions, without doing a full
find on the system and without being limited to the directories that
whereis looks at.  Finder itself, however, is limited to a search on
directory names.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	finder
#	finder.1
#	mkfindex
# This archive created: Tue Jun 28 22:45:59 1988
echo shar: extracting README
cat << \SHAR_EOF > README
Ever wonder if anyone else on the system has implemented that software
package?  Ever wonder where the subdirectories relevant to something
are?

Finder is a quick way to answer these questions, without doing a full
find on the system and without being limited to the directories that
whereis looks at.  Finder itself, however, is limited to a search on
directory names.

The finder package is implemented as /bin/sh scripts using find and
awk.  You may want to customize the find command in mkfindex -- for
example, not to cross mount points (-xdev, if you have it).  Perl would
probably do the job even faster than awk and if anyone translates this,
I'd like a a copy.

To get an idea of how big the index files (.findex) are, the one at the
root of our system covering 2.8 gigabytes of disk space in use is about
145 kilobytes.

---------------------------------------------------------------------------
|  .,, >{~)   ['} /           Stephen Kinzler           \ + < : ~.$[~{}== |
| %  > %()}@!  ' /     kinzler@iuvax.cs.indiana.edu      \ '}~{ >;' & )<} |
| <@;"??{.,':}  /   {rutgers,gatech,att}!iuvax!kinzler    \ &"(&@=+}<+;@) |
|  ]>)"}  +{[) /   Indiana Univ Dept of Computer Science   \  @ }%[^??^&. |
| !  %% ~!,!  / 339 S Lincoln #C; Bloomington, IN 47401 USA \ ,: (>..<*%$ |
---------------------------------------------------------------------------
SHAR_EOF
echo shar: extracting finder
cat << \SHAR_EOF > finder
#! /bin/sh

# finder - find directories
# searches an index file at the root of each specified directory (default
# current directory) to find the pathnames of all directories within the
# specified directories whose name matches the given regular expression
# see also: mkfindex
# Stephen Kinzler, kinzler@iuvax.cs.indiana.edu, June 1988

exp=${1?"usage: $0 expression [ directory ... ]"}; shift

for dir in "${@-.}"
do
	cd "$dir"
	test "$dir" = / && dir=

	awk -F/ '       { depth = $1; path[depth] = $2 }
	$2 ~ /'"$exp"'/ { printf "'"$dir"'"
	                  for (d = 1; d <= depth; d++)
	                      printf "/%s", path[d]
	                  printf "\n" }' .findex
done
SHAR_EOF
echo shar: extracting finder.1
cat << \SHAR_EOF > finder.1
.TH finder 1-IUCS
.SH NAME
finder, mkfindex \- find directories
.SH SYNTAX
.B finder
expression [ directory ... ]
.br
.B mkfindex
[ directory ... ]
.SH DESCRIPTION
.B Finder
searches an index file
at the root of each specified directory
(default current directory)
to find the pathnames of all directories
within the specified directories
whose name matches the given regular expression.
It's report is only as up to date as the index file.
.PP
.B Mkfindex
searches the specified directories
(default current directory)
to make an index file of subdirectory names
at the root of each directory called
.I .findex.
The index file is only as complete
as permissions in the directories allow.
.SH FILES
 .findex
.SH "SEE ALSO"
find(1), whereis(1)
.SH AUTHOR
Stephen Kinzler
.br
kinzler@iuvax.cs.indiana.edu
.br
{rutgers,gatech,att}!iuvax!kinzler
SHAR_EOF
echo shar: extracting mkfindex
cat << \SHAR_EOF > mkfindex
#! /bin/sh

# mkfindex - make finder index files
# searches the specified directories (default current directory) to make
# an index file of subdirectory names at the root of each directory
# see also: finder
# Stephen Kinzler, kinzler@iuvax.cs.indiana.edu, June 1988

trap 'rm -f /tmp/findex$$; exit 1' 1 2 15

for dir in "${@-.}"
do
	cd "$dir"
	test "$dir" = / && dir=

	find . -type d -print 2> /dev/null | \
	    awk -F/ '{ printf "%d/%s\n", NF - 1, $NF }' > /tmp/findex$$ &&

	mv /tmp/findex$$ .findex ||
	    echo "$0: $dir findex file still in /tmp/findex$$" 1>&2
done
SHAR_EOF
chmod +x finder mkfindex
#	End of shell archive
exit 0