LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-08-2008, 12:47 PM   #1
GoTerpsGo
LQ Newbie
 
Registered: Mar 2008
Posts: 3

Rep: Reputation: 0
Question Grepping for text within files


Strangely I can't seem to google for a satisfactory answer or for one that works.

I recently inherited a set of Apache web servers that have a bunch of virtual host config files and I want to find one containing the string "mgh". These config files are spread across multiple directories and I have no way of contacting my predecessor as to where the file may be. I found some posts advocating use of Perl for search and replace; I found another one using grep with a bunch of options (it didn't work). Is there a concise way of doing this down many levels from a parent directory?

- Joe
 
Old 09-08-2008, 01:21 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,504

Rep: Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992
Quote:
Originally Posted by GoTerpsGo View Post
Strangely I can't seem to google for a satisfactory answer or for one that works.

I recently inherited a set of Apache web servers that have a bunch of virtual host config files and I want to find one containing the string "mgh". These config files are spread across multiple directories and I have no way of contacting my predecessor as to where the file may be. I found some posts advocating use of Perl for search and replace; I found another one using grep with a bunch of options (it didn't work). Is there a concise way of doing this down many levels from a parent directory?

- Joe
If you have one directory, with a bunch of subs under it, and want to search all the .config files for "mytext":

cat `ls -alR *.config` | grep -H mytext > output.txt

Should leave you with an output.txt file, with grep results and file names (I think the -H is correct...not looking at the grep man page right now...).

Also note those are backticks (key to the left of the 1 on the keyboard), NOT single-ticks (on the key with the double-quotes).
 
Old 09-08-2008, 01:43 PM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
grep -R mgh /your/path
edit: if they all have the same name (or .conf or similar extensions), then find may be more use:
Code:
find /your/path -iname "*.conf" -print0 | xargs -0 grep mgh

Last edited by pwc101; 09-08-2008 at 01:47 PM.
 
Old 09-08-2008, 01:48 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,504

Rep: Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992Reputation: 3992
Quote:
Originally Posted by pwc101 View Post
Code:
grep -R mgh /your/path
edit: if they all have the same name (or .conf or similar extensions), then find may be more use:
Code:
find /your/path -iname "*.conf" -exec grep mgh {} \+
Always more than one way to do something... Good stuff!
 
Old 09-08-2008, 01:59 PM   #5
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Oops I see the ground fas already been covered

Last edited by john test; 09-08-2008 at 02:03 PM.
 
Old 09-08-2008, 02:10 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
grep -r will work. Add -i if you are not sure about the case of the string.
 
Old 09-09-2008, 07:52 AM   #7
smehra24
Member
 
Registered: Jan 2008
Location: mumbai
Distribution: red hat
Posts: 31

Rep: Reputation: 15
grep -ir search_string directory_to_search_in
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DOS text files to UNIX text files ta0kira Linux - Software 7 03-15-2011 11:42 AM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-07-2007 11:24 PM
Application to convert MySQL files MYD, MYI, FRM to SQL text files ? SethShannon Linux - Newbie 1 09-28-2007 06:03 PM
Grepping a file for Text going down. keysorsoze Linux - Newbie 3 01-17-2007 09:06 AM
grepping unknown text, is there a way? vdemuth Programming 15 09-26-2005 03:00 AM


All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration