Search Utility

In the course of my job I often need to search for a particular string of text which may appear in any of hundreds of different files beneath a particular directory node. The files may be either gzip conmpressed or non-compressed, and in the case of the gzipped files, there may or may not be a .gz suffix on the filename.

There is no built in facility to do this on a unix system, so I have written a couple of scripts to do the job. The scripts are as follows :-

% cat search
echo "Checking normal files"
find . -name '*' -exec fgrep "$1" {} /dev/null \; 2> /dev/null
echo "Checking compressed files"
find . -name '*' -exec checkit {} "$1" \; 2> /dev/null

And the checkit script which is used by search is as follows :-


gzip -dc $1 | grep $2 > /tmp/asdf1
size=`stat -c %s /tmp/asdf1`
if [ $size -gt 0 ]
         echo $1 $2

Then all you need to do is make sure both search and checkit have execute permission and are located in your path. You will need to be logged in as root to issue the commands below.

cp search /usr/bin
cp checkit /usr/bin
chmod 777 /usr/bin/search
chmod 777 /usr/bin/checkit

Alternatively you place the files in a directory underneath your home directory e.g /home/me/bin

Then you need to make sure your environment includes this directory in your path.

export PATH=$PATH:/home/me/bin

Let's see it in action

% search "testing"
Checking normal files
Binary file ./Customer/Products/java/JMSAdapter/lib/weblogic.jar matches
Binary file ./Customer/Products/java/JMS/weblogic.jar matches
Checking compressed files
./Customer/Products/asdf.gz testing

Very handy indeed.


QR Code
QR Code search_utility (generated for current page)