Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to grep a string in a directory tree with arbitrary depth? 2

Status
Not open for further replies.

cyan01

Programmer
Mar 13, 2002
143
US
There are multiple ascii files in a directory tree with arbitrary depth, for instance:

$HOME/file1.txt
$HOME/dir_1/.../dir_n/file_b.txt
......
......
$HOME/yyy_1/.../yyy_m/file_a.txt

How can I write a unix script to grep a string, say 'this string', from the directory tree listed above?

Note: I don't know how many sub-directories under $HOME, nor do I know the filenames and directory names. But I need to grep a given string from all files under $HOME.

Many thanks!
 
Hi ,

One simple option can be ,

find $HOME -exec grep -i "your string" {} \;

or if you want to see just the file names with your pattern,

find $HOME -exec grep -il "your string" {} \;

Cheers
 
Thank you for your prompt help!

I just tried the command on Solaris 8. It seems not working :-(. The process just hangs there and never completes. Nothing is returned.
 
Here is my detailed tsting output:

%find . -exec grep -i| "this string" {}\;
find: incomplete statement
smith: Command not found

%find . -exec grep -i "this string" {}\;

The above command just hangs there. Nothing is returned.
 
You seem to have a | (vertical bar) instead of an lower case l (ell) as a parameter for the grep and you are missing a space before the \.

Try cutting and pasting parbhani's solution. It seems correct to me, although I don't have a unix system to try it on at the moment.

CaKiwi
 
I know the answer now.

If using solaris grep, the command is:

%find . -exec grep "smith" {} \; -print

If using gnu grep, then

% find . -exec grep "smith" {} \;

will be fine.

Thank you all very much!

 
How about either print out the file names:
Code:
find . -type f | xargs grep -l xxx
Or print out each file contents:
Code:
find . -type f | xargs grep -n hello
Or something similar. Cheers, Neil
 

Does the "grep -R string *" command that stefanwagner posted on May 3rd work? I tried on our Sun Solaris8 platform and there is not a -R option for recursive search, so I can only see the other solutions working for us (i.e. find . -exec grep...) Has anyone found a recursive option for grep or egrep in Solaris? Thanks, KenDogg
 
Ken,

I'm not sure what other flavors of UNIX support the -R option, but Linux supports it. We don't have it on our HP-UX 10.20 or Solaris servers.

John
 
On linux there is a rgrep, which is the same as grep -r. (Both: gnu)

And won't gnu-grep compile on HP-UX, Solaris, ...
I always get huge makefiles with sections for a dozen of platforms :) - don't know this for grep, since I don't have the source available.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top