Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-16-2004, 09:09 AM   #1
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.
Old 04-16-2004, 09:20 AM   #2
Registered: Mar 2004
Location: Australia
Distribution: Debian, Ubuntu
Posts: 55

Rep: Reputation: 15
Something like this, maybe?
grep -Hn string `find / -name *.ext`
Old 04-16-2004, 09:45 AM   #3
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, 09:46 AM   #4
Senior Member
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,254

Rep: Reputation: 60
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 {} \;

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.


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 09:23 PM
Simple SH Error - I hope... Micah Programming 5 06-28-2005 11:41 PM
Simple Question (I hope) jon3k Linux - General 3 04-13-2005 03:51 PM
Simple Text Reformat (sed and grep) mikefazz Programming 2 08-31-2004 08:38 PM
Simple grep command problem acis Linux - General 2 07-16-2004 01:21 PM > Forums > Linux Forums > Linux - Newbie

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

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