Etusivu > ATK > GNU/Linux > superfind

superfind

superfind on monipuolinen tiedostojen hakuohjelma, jonka olen ohjelmoinut bash-komentotulkin kielellä.

Koodi

#!/bin/sh
#
# superfind - search for files with different criteria
#
# $Id: index.html.ht,v 1.4 2003/02/04 20:29:02 mtreinik Exp $
#
# Copyright (c) 2001 Mikko Reinikainen 
# (student id 46645A)
#
# EXTRA CREDIT:
#  * --local argument gives its option to the find command
#
# EXAMPLES:
#  $ ./superfind
#  $ ./superfind -d ~/ bar
#  $ ./superfind -d /home/foobar -n '\.ps$' PostScript duplex
#
# BUGS:
#  * multiple occurrences of a regexp on a line are only counted once
#  * specifying multiple directories, names or types may display
#    the same file many times

prog="$0"
usage="\
Usage: $prog [-h] [--help] [-r] [--no-recursion] [-d dir] 
       [--directory dir] [-l param] [--local param] [-n name] 
       [--name name] [-t type] [--type type] [regexp...]

Super search for files. Performs a recursive search for files in the
specified directory. You can specify multiple directories, names, file
types or content regexps.

Options:
        -h, --help              display this help and exit
        -d dir, --directory dir specify directory where the search begins
        -l param, --local param give param to find command
        -n name, --name name    search only file names containing name
        -r, --no-recursion      search only in the specified directories
        -t type, --type type    search only files of type type
"
maxdepth=
dir=
name=
type=
regexp=
findparam=

# process command line arguments
while test $# -gt 0; do
  case "$1" in
  -h|--help|--h*)
    echo "$usage"
    exit 0
    ;;
  -r|--no-recursion)
    maxdepth="-maxdepth 1"
    ;;
  -d|--directory)
    shift
    test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
    dir="$dir $1"
    ;;
  -l|--local)
    shift
    test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
    findparam="$findparam $1"
    ;;
  -n|--name)
    shift
    test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
    name="$name $1"
    ;;
  -t|--type)
    shift
    test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
    type="$type $1"
    ;;
  *)
    regexp="$regexp $1"
    ;;
  esac
  shift
done

# if no directory was specified, use the current working directory
test -z "$dir" && dir=.

# find all files under the specified directories
files=`for d in $dir; do 
         find $d $findparam -type f $maxdepth
       done`

# select only named files
if test -z "$name"; then
  files=$files
else
  files=`for f in $files; do
           for n in $name; do
             if ( basename $f | grep $n >/dev/null ) ; then
               echo $f
             fi
           done
         done`
fi

# select only specified file types
if test -z "$type"; then
  files=$files
else
  files=`for f in $files; do
           for t in $type; do
             if ( file $f | grep $t > /dev/null ) ; then
               echo $f
             fi
           done
         done`
fi

# look for regular expressions in files 
if ! test -z "$regexp"; then

  # count regexp instances
  files=`for f in $files; do
           echo \`for r in $regexp; do grep $r $f; done |wc -l\`\;$f
         done`

  # sort files by number of regexp instances
  files=`( for i in $files; do echo $i; done ) |sort -n -r`
fi

# print results
for i in $files; do echo "$i" |sed 's/;/	/'; done
Sivun viimeisin muutos: "2003-02-04 22:29:00 mtreinik"