LinuxQuestions.org
Help answer threads with 0 replies.
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 04-16-2004, 08:09 AM   #1
MiguelE
LQ Newbie
 
Registered: Apr 2004
Posts: 1

Rep: Reputation: 0
Simple grep question - I hope !


Hopefully this is a simple question for someone with a bit more linux skills than me (not hard !)

I need to run a command that will search through all the file systems for files with a certain extension, and then search within these files for a certain text string. I would like the returned results to show the path and name of the file, plus the line containing the text.

I think I need a combination of find and grep, but cant seem to work out the right parameters.

Any help much apprieciated.
M
 
Old 04-16-2004, 08:20 AM   #2
sims
Member
 
Registered: Mar 2004
Location: Australia
Distribution: Debian, Ubuntu
Posts: 55

Rep: Reputation: 15
Something like this, maybe?
Code:
grep -Hn string `find / -name *.ext`
 
Old 04-16-2004, 08:45 AM   #3
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
If your version of locate can do regular expressions (?the -e option) then do:

locate -e '.extension$' | xargs grep 'text string'

locate finds all the things ending in .extension; and xargs turns locate's output into arguments for grep - otherwise grep would just search the output of locate for 'text string'.

 
Old 04-16-2004, 08:46 AM   #4
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,186

Rep: Reputation: 52
if bash complains about
grep -Hn bugger `find / -name *.txt`
find: paths must precede expression
Usage: find [path...] [expression]

add an quotes around *.txt like "*.txt"

my favorite, but harder to type is

find / -name "*.txt" -type f -exec grep -Hn {} \;

explained.
find, starting at the root directory, filenames matching *.txt, of type file (not directory or process), and on those names returned, execute grep -Hn, {} -the results of the find, then terminate the command \; spacing is important at the end.
 
  


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
simple C++ string questioin, i hope Ha1f Programming 2 10-12-2005 08:23 PM
Simple SH Error - I hope... Micah Programming 5 06-28-2005 10:41 PM
Simple Question (I hope) jon3k Linux - General 3 04-13-2005 02:51 PM
Simple Text Reformat (sed and grep) mikefazz Programming 2 08-31-2004 07:38 PM
Simple grep command problem acis Linux - General 2 07-16-2004 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:19 PM.

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