#!/bin/env sh # # /usr/local/bin/grepdir # # Author: Chris Karakas # http://www.karakas-online.de # # Searches the files of directory DIR recursively for # the expression EXP, excluding the contents of EXCLUDE_DIR # from the search, if any. # # Usage: grepdir EXP DIR [EXCLUDE_DIR] # # Copyright (c) 2003, Chris Karakas # http://www.karakas-online.de # chris at mydomain dot de (see above for my domain) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. # Help function # function help() { cat <<-EOF Usage: grepdir EXP DIR [EXCLUDE_DIR] Searches the files of directory DIR recursively for the expression EXP, excluding the contents of EXCLUDE_DIR from the search, if any. -h, --help Display this help text EOF } # Check arguments and issue a help statement, if wrong # if [ $# -eq 0 ]; then help exit 1 elif [ "$1" = "-h" -o "$1" = "--help" ]; then help exit 0 elif [ $# -eq 1 -o $# -gt 3 ]; then help exit 1 fi FIND="/usr/bin/find" XARGS="/usr/bin/xargs" GREP="/usr/bin/grep" # Program name PN=${0##*/} arg1=$1 shift if [[ X"$2" = X"" ]]; then echo "$PN: running $FIND $1 -type f -print0 | $XARGS -0 $GREP \"$arg1\"" $FIND $1 -type f -print0 | $XARGS -0 $GREP "$arg1" else echo "$PN: running $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP \"$arg1\"" $FIND $1 -path $2 -prune -o -type f -print0 | $XARGS -0 $GREP "$arg1" fi