Linux:Script to use find to search contents of files

By Eric Downing. Filed in Linux, OS, Scripting, Shell, Utilities  |  
TOP del.icio.us digg

I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like:

find . -exec grep "somestring" {} ; --print

This find command will search from the current directory and for each file will run grep on it and if it finds a matching string, it will print the string and then the filename. So I took this and moved it into a script.

#!/bin/sh

if [ $# -lt 1 ]; then
  echo 1>&2 Usage: $0 ""
  exit 127
fi
icase=
search=

while [ $# -ge 1 ]; do
  case $1 in
     -i) icase=$1;;
      *) search=$1 ;;
  esac
  shift
done

find . -type f -print0 | xargs -0 grep $icase $search

This allows me to run a command such as

ffind -i Super

This command will search all file names starting from the current directory and recurse directories and the “-i” will tell it to ignore case.

Tags:

Leave a Reply