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

Find string in any file on any directory 2

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
I need to find all occurrences of a particular string "Error code 27" from the whole server? I also need the file name associated with it.

I tried the following command, but I only got the results from the current directory: > grep "Error code 27" *

I need to go to all sub-directories and find this particular string, how would you do it?

Thanks!
 
find . -type f | xargs grep 'Error code 27'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Q: I need to go to all sub-directories and find this particular string, how would you do it?

A: You find it...


HTH,

p5wizard
 
Thanks PHV!
Is it possible to echo the filename as well?
 
Not tested, but maybe add an -exec ls {} to the find?

I want to be good, is that not enough?
 
find . -type f | xargs grep 'Error code 27' /dev/null

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

pdtak said:
Is it possible to echo the filename as well?
Maybe would be the moment to tell us what kind of [tt]grep[/tt] implementation are you using.

For example my GNU [tt]grep[/tt]'s default is to display the file name too if multiple input files were given. But anyway, it is possible to ask [tt]grep[/tt] to always display the file name :
man grep said:
-H, --with-filename
Print the file name for each match. This is the default when
there is more than one file to search.


Feherke.
 
You might have problems if your filename contain spaces

if your find command support it
find . -type f -exec grep 'Error code 27' /dev/null {} +

else
find . -type f -exec grep 'Error code 27' /dev/null {} \;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top