1826216 Members
2828 Online
109691 Solutions
New Discussion

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
jilpangs
Occasional Advisor

Re: Unix Grep

Hi,

I'm not sure which file would have the string which i'm searching for. Hence i'm planning to start the search from my home directory.

Thats why i couldnt "Limit to set of directory"

How can limit to set of directory ?

Thanks
Jil
A. Clay Stephenson
Acclaimed Contributor

Re: Unix Grep

find /dir1 /dir2 /dir3/xxx -type f ...

This will sequentially search /dir1 (and all subdirectories) then /dir2 (abd subdirectories) then /dir3/xxx and so on.


Man find for details.

If you are unsure, then you can simply find / but be aware of the load you are imposing on the system.
If it ain't broke, I can fix that.
Philip Kernohan
Advisor

Re: Unix Grep

jilpangs,

I use something similar to this when I'm looking for a text string in a list of directories. You obviously need the correct search/read permissions to do this:

find . -follow -type f \( -name '*.sql' -o -name '*.sh' -o -name '*.log' \) -exec grep -il '' {} \; 2>/dev/nu

find
. :from this direction

-follow :follow all symbolic links

-type f :find only regular files

\( -name ... -o \) :matching an OR of these regular expressions

-exec :execute the following command

grep -il '' :grep all find results for regular expression, ignore case and print filenames to stdout

2>/dev/null :stderr to /dev/null

You can place this is a 'for' loop to execute more commands on the resulting files.

Regards,
PK
It's nice to be important but it's more important to be nice
Hein van den Heuvel
Honored Contributor

Re: Unix Grep


Do you need to 'see' the matches, or just know which files might match. Fo rthe latter:

find . -type f \( -name '*.sql' -o -name '*.sh' -o -name '*.log' \) -exec grep -l "ADFH Script Failed" {} \;

Hein.