1834805 Members
2426 Online
110070 Solutions
New Discussion

Google for Unix System

 
SOLVED
Go to solution
Hunki
Super Advisor

Google for Unix System

I have a need to search the system for a file which should have a certain string... is this a good way to search for it :

cd /
grep -i "string" */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/* 2>/dev/null

Does anybody know a better way to search the whole system for certain string in a file, when the file in which the string is placed is unknown ...
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Google for Unix System

Hi:

If you truly want to search every file in a directory and 'grep' for a paricular string, I'd do:

# find / -type f | xargs grep "Hunki"

This will find *files* with the string "Hunki" somewhere within and report both the filename and every matching instance.

One of the problems with this, however, is that you will search binary files along with ASCII (text) files. Searching binary files is fruitless.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Google for Unix System

Hi (again):

Actually, we can rather quickly circumvent searching the non-ASCII (binary) files :

# cat ./google
#/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -type f | while read FILE
do
[ `file ${FILE}|grep -c ascii` -eq 0 ] && continue
grep "$PAT" ${FILE} /dev/null
done

...run the script passing the directory ($1) that you want to search and the pattern ($2) you want to find. For example:

./google /etc/rc.config.d ROUTE

Regards!

...JRF...
Hunki
Super Advisor

Re: Google for Unix System

I am actually looking at searching the whole system and not just a single directory or a directory structure. Any more inputs would be highly appreciated.
Patrick Wallek
Honored Contributor

Re: Google for Unix System

The method JRF describes will for specific directories or the whole system.

If you want to search your entire system then just use / for your directory. That will cause the find command to traverse the entire system.

Just be wanred that this MAY slow your system as find can be CPU intensive.
A. Clay Stephenson
Acclaimed Contributor

Re: Google for Unix System

James' solution will search the entire system -- simply supply / as the starting directory (${1}) and it will go from there. It may take a while but it will slug through the system.
If it ain't broke, I can fix that.
Steven E. Protter
Exalted Contributor

Re: Google for Unix System

Shalom Hunki,

I urge caution.

It can only take a few find processes searching from root to bring a powerful system to its needs.

Going through every file on a system can consume a lot of resources. Its better to search the file systems most likely to contain the information individually.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Yogeeraj_1
Honored Contributor

Re: Google for Unix System

hi hunki,

running the search on all files on the file system can be very time consuming and is also not recommended on a production system.

you would not want to search /dev/, /tmp/ and /stand for example!

You may also restrict the search on certain type of files only.

please check

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Deepak Kulkarni
Regular Advisor

Re: Google for Unix System

Hi,

As told above it is not recomended to serach string from the root filesystem. Eventhough if you are going for that make sure sufiicient memory is present.

Cheers
Deepak

Hunki
Super Advisor

Re: Google for Unix System

A little change in requirement :

I dont want to search *log* / binaries while searching.

thanks,

hunki
Pete Randall
Outstanding Contributor

Re: Google for Unix System

A minor modification of the find command should do the trick. Using James' google script:


# cat ./google
#/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -type f ! -name "*log" | while read FILE
do
[ `file ${FILE}|grep -c ascii` -eq 0 ] && continue
grep "$PAT" ${FILE} /dev/null
done


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Google for Unix System

Oops, I missed an asterisk. Make that

! -name "*log*"


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Google for Unix System

hi Hunki:

If I understand correctly, you don't want to search either binary files *or* files with the string "log" in their name. If that is corerct, use this modified version of my original script:

# cat ./google
#/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -type f -a ! -name "*log*" | while read FILE
do
[ `file ${FILE}|grep -c ascii` -eq 0 ] && continue
grep "$PAT" ${FILE} /dev/null
done
exit 0

...run the script passing the directory ($1) that you want to search and the pattern ($2) you want to find. For example:

./google /path pattern

Regards!

...JRF...