Unix Grep

 
jilpangs
Occasional Advisor

Unix Grep


Hi,

I need to search for a string through out
my system. i.e as soon as i login my unix box,
i reach my home directory. From there , i need
to issue a search command which needs to search all scripts (*.sql, *.sh, *.log etc..) under all directories and sub directories.

home_dir > grep "ADFH Script Failed"

Can anyone let me know , what command should i issue.

Thanks
Jil

13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor

Re: Unix Grep

cd to desired starting directory; / ig desired

TARGET="ADFH Script Failed"
find . -type f \( -name '*.sql' -o -name '*.sh' -o -name '*.log' \) | while read X
do
grep -q "${TARGET}" ${X}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${X}"
fi
done

If it ain't broke, I can fix that.
jilpangs
Occasional Advisor

Re: Unix Grep

Hi,

I copied your script into a shell script(search.sh) and executed it from my home directory.

It returns only "search.sh" contains the target.I know that the traget string which i mentioned is also available in one of the subdirectory, which is not returned by this script.

Thanks
Jil
Rodney Hills
Honored Contributor

Re: Unix Grep

Is that subdirectory pointing to by a symbolic link?

find will not traverse symbolic links unless you add the option "-follow"

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Unix Grep

If you are looking to search your entire system, then change the "find" to-

find / -type f \( -name '*.sql' -o -name '*.sh' -o -name '*.log' \) | while read X

I am wondering why you need to search the whole "system" since you'll be re-grepping the same files over and over and wouldn't need to be.

Anyway you could limit your search to specific directories, or at least files that with a recent modification date?

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Unix Grep

The script as written will search subdirectories as well but it must find an exact match to "ADFH Script Failed"; no other pattern will be matched. Spaces and case matter. You could make the grep case-insensitive by adding a "-i" parameter to the grep.

As with all scripting, it's best to break apart the commands to see if they are doing what you think they are. I would try the find command by itself to see if it is returning the list of files matches the criteria first.


find . -type f \( -name '*.sql' -o -name '*.sh' -o -name '*.log' \)

If that works then you need to look at the grep command.
If it ain't broke, I can fix that.
jilpangs
Occasional Advisor

Re: Unix Grep

I dont understand , when you say it will search again and again...

For eg:
These are the directories and subdirectories in my system.

home_dir >
home_dir/one >
home_dir/one/one_a >
home_dir/two >
home_dir/two/two_a >

When i issue a search command from
home_dir >
It has to search home_dir >
then
home_dir/one >
then
home_dir/one/one_a >
etc......

Why would it search again and again the same directories ?

Thanks
Jil

Rodney Hills
Honored Contributor

Re: Unix Grep

When you said "search for a string though out my system", I thought you were talking about your entire system. You really want to search your entire "home directory" it sounds like.

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: Unix Grep

It is almost always dumb to search from the root directory because find can place a huge load on the system especially if several are running at once. You will be much better served by specifying a more limited set of directorys hence the suggestion to cd to the desired starting directory. I often create a wrapper around the find comand to prevent find / searches by regular users.
If it ain't broke, I can fix that.
jilpangs
Occasional Advisor

Re: Unix Grep

Sorry. I didnt make it clear to all.
But thats my requirement. Needs to
search all directories and subdirectories under my home directory.

Can anyone help with code ?

Thanks
Jil