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!

simple script question

Status
Not open for further replies.

Nostradamus

Technical User
May 3, 2000
419
SE
I'm using SCO Openserver 5.0.5
I'm new to unix and have a simple problem.

I want to search all files in a directory with several subdirectories. I then want to grep that output for a specific word.
/Sören
 
find is your friend ;-)
[tt]
# cd directory
find . -type f | xargs grep -l "word"
#[/tt]

This will print a list of all the files that contain word

NB: If there's a lot of files/directories, this could take some time!

HTH One by one, the penguins steal my sanity. X-)

 
If you are using SCO, you should use the SCO forum for more specific questions (this one is fine here or there)

But it actually isn't a simple question :)

The general answer is to use find, and the simplistic approach is


find /whereveryouwantostart -exec grep whatever {} dev/null \;

That's not necessarily very efficient. Using xargs can help

find . | xargs grep whatever

But it also has bugs if the filenames could have "-" at their beginning. Fixing that can be a little nasty.

You may not want to grep binary files:

find . -type f -print|xargs file|grep -i text|cut -fl -d: | xargs grep whatever

That's pretty awful, but it's what you have to get into if you have special cases. Special cases are what makes this question more difficult. If you have a small number of files and subdirs to search, the simple approach may work fine for you. If not, you have to get more creative.
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Oh, and sometimes the brute force approach is good enough- if I know there is only one level of sub-directory under me with not a lot of files, I might just do:

grep pattern * */* */*/*

But don't try that in your / directory ! Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
I didn't use the SCO forum since a lot of script questions are asked here. The result of that is that here is where the script guru's are found.
Actually it was even easier then I thought. Even I, with my limited experience managed to find it out.
I used... grep word `find . -name file`
(the name of the files I was looking for had the same name)

The -exec parameter to find is excellent though. I never thought of that.
I'll also look into the xargs command which will come in handy at times.

thanks. /Sören
 
Ayup.

I think it's not at all a bad idea to put script questions here. I just want to make sure you know about the SCO forum because I have seen people post specific SCO issues here.

It is also important to say it's SCO or whatever as you did, because many commands vary slightly in syntax on different versions.

Given the difficulty of writing a really good recursive grep using shell tools, I'm surprised no one has taken the truble to write such a utility- it would be quite useful.
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Yeah, but that's still a script: it just slightly automates what you'd do by hand and doesn't at all address the underlying problems.

A good utility would automatically ignore binary files unless told not to, wouldn't be upset by funky file names, named pipes etc., and wouldn't have the overhead of 3 commands running.

Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top