Grep Problem

Hi,
I want search all files containing some "searchtext" in current dir and sub dir. For this, I used

grep "searchtext" */*

This will search only in sub dir files and does not give results if file containing search text exists in current directory.

and

grep "searchtext" *

This will search only files current directory, not sub directories.

But when I use , following command . I get result.

find . | xargs grep "searchtext"
It searches at both current dir and sub dir .

How to achive this using grep command .

thanks

Arent you using grep in your command ?

Or do you just want to use grep without the find ?

vino

Hi,

I want to use only grep command to search keyword in all files (current dir and sub dir)

Thanks

find has a command option that allows you to run, for instance, a grep against each file it locates. This is sometimes very useful... You'll have to check the man page for the syntax.

Here is your answer

find . -name 'haystack' -exec grep 'needle' {} \; -print

The -print will display the corresponding haystack for each successful needle found.

Vino

... i find it quiet interesting that when given the whole gamut of Unix tools that are available to do things with, there are people who insist on only using 1 tool and 1 tool only even when there are other tools that might be able to achive the required results quicker --- unless this is homework ....

Just Ice,

for learning point of view (atleast for me), how would you approach the OP to suit your statement.

Thanks,
vino

perl or find-awk or awk comes to mind ... of course, i'm not that good with perl but the few examples i've seen on this forum have been very fast in what they were designed to do ...

however, the point i was making is that people should not restrict their choices to certain tools only --- people who do will inevitably find their options restricted by their inflexibility ... obviously this is not the case for homework questions as those were meant to have restrictions ...

You can use grep --version to tell which version you are on.

the GNU grep I have (2.5a and 2.5.1) can use -r for a recursive search.

grep -r "search text" * will search the current directory and all sub-directories underneath.

You can always man grep and look for either -r, -R or -d recurse options. Not all versions of grep will use these.

;