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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grepping a string across mutiple directories

Status
Not open for further replies.

DanAuber

IS-IT--Management
Apr 28, 2000
255
0
0
FR
Having a bad day...

Is there a one-liner to grep a string for example 'Find this text' looking in all files from the root directory downwards ? (i.e. looking across the whole server)

thanks for any help

dan
(HPUX 10.20)



Dan Auber
DanAuber@aol.com
 
Hello Dan,

This one is a bit sticky, so I don't usually recommend it from the root directory. It seems that grep(at least in Solaris 4.5.1) will get lost in an ELF file and some others I'm sure.

Anyway, try: grep yourstring */*/*/*

What happens is grep will search all files as far down as you specify from the current location. Grep will complain of you specify too many levels and of course seems to get lost on certain file types which makes grepping from places like /etc or /dev problematic.

Check the man page to set the flags to get the output you want. This one should display the path/filename:line matched format.

Later!
Charlie Ehler
 
Hi:

If you really want to search from root, I'd do something like this:

find . -type f -print | xargs file | grep -i text |
cut -f1 -d: | args egrep "$*"

Some thoughts:

1) xargs ensures standard input doesn't overflow.
2) 'file|grep -i text' searches for text files files only.
3) cut the file name from output of the grep
4) egrep the text files for expression $*

Regards,


Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top