LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 02-21-2008, 08:31 AM   #1
tejama
Member
 
Registered: Aug 2007
Posts: 37

Rep: Reputation: 15
Help with grep (or similar command)


What command can I use to search through a directory of files and display any file names that contain a certain string in it's text?

Hope that's clear and thanks in advance.
 
Old 02-21-2008, 08:35 AM   #2
timnp
Member
 
Registered: Feb 2008
Location: London
Distribution: FC8, FC9, Centos 4, Centos 5, Knoppix
Posts: 52

Rep: Reputation: 17
Lots of ways to do this.

If you must do it with grep then do this...

find | grep PartsOfFileName

Or did you mean that the text has to be acutally in the file? The above finds file with the text in their names/paths

Last edited by timnp; 02-21-2008 at 08:36 AM.
 
Old 02-21-2008, 08:37 AM   #3
tejama
Member
 
Registered: Aug 2007
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks for the quick reply.

I'm actually looking to find text in the body of the file.
 
Old 02-21-2008, 09:05 AM   #4
catbird
LQ Newbie
 
Registered: Oct 2006
Posts: 26

Rep: Reputation: 16
Quote:
Originally Posted by tejama View Post
What command can I use to search through a directory of files and display any file names that contain a certain string in it's text?

Hope that's clear and thanks in advance.
find . -iname "filename*" -print0 | xargs -0 ls -ld > out.txt
never mind ... you don't want this. You want to search inside files. Sorry

Last edited by catbird; 02-21-2008 at 09:18 AM.
 
Old 02-21-2008, 09:34 AM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try:

grep phrase_you_are_looking_for file
 
Old 02-21-2008, 09:52 AM   #6
timnp
Member
 
Registered: Feb 2008
Location: London
Distribution: FC8, FC9, Centos 4, Centos 5, Knoppix
Posts: 52

Rep: Reputation: 17
Maybe this:

grep -lir "the text to search for in quotes" *

-l makes it just list the filenames and paths
-i makes it case insensitive. can be much faster if you remove this
-r makes it search in subfolders etc

Have any of these suggestions helped you find your file yet?
 
Old 02-21-2008, 10:47 AM   #7
tejama
Member
 
Registered: Aug 2007
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sycamorex View Post
grep phrase_you_are_looking_for file
Nope...this isn't what I was looking for at all.
 
Old 02-21-2008, 03:35 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
I'm actually looking to find text in the body of the file.
Isn't it what 'grep text_you_are_looking_for file' does?

If not, can you be more specific?
 
Old 02-21-2008, 03:47 PM   #9
griffey
Member
 
Registered: Jan 2004
Location: East Central Illinois
Distribution: RHEL 4/5/6 and Fedora
Posts: 89

Rep: Reputation: 15
How about:

grep "string_you_are_looking_for" -lr /path/to/directory

-r is for looking recursively through all subdirectories as well
-l (lowercase L) tells it (IIRC) to stop at the first instance of a match (rather than continuing to search the file) and then display the entire path to the file

Good luck.

G.--
 
Old 02-21-2008, 09:28 PM   #10
tejama
Member
 
Registered: Aug 2007
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sycamorex View Post
Isn't it what 'grep text_you_are_looking_for file' does?

If not, can you be more specific?
I'm looking to output a list of files that have a certain string in the body. I believe your command just searches an individual file that you have to specify.
 
Old 02-21-2008, 10:46 PM   #11
frndrfoe
Member
 
Registered: Jan 2008
Distribution: RHEL, CentOS, Ubuntu
Posts: 379

Rep: Reputation: 38
err..
Did you see the post from timnp?
 
Old 02-22-2008, 12:09 AM   #12
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Cool

Quote:
Originally Posted by tejama View Post
I'm looking to output a list of files that have a certain string in the body. I believe your command just searches an individual file that you have to specify.
grep -rl <string> ./
this command will show the list of files under current directory which are having the string in the body
 
Old 02-22-2008, 06:32 AM   #13
tejama
Member
 
Registered: Aug 2007
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by frndrfoe View Post
err..
Did you see the post from timnp?
Sorry...somehow skipped over this timnp's post. Much thanks to you all (especially timnp!)
 
  


Reply



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
unix command thread status similar to ps powah Linux - Newbie 1 09-29-2006 10:48 AM
How to grep similar lines in bash? bruno buys Linux - Software 2 12-02-2005 11:56 PM
CLI....ls (or similar command)...formatting output Basslord1124 Linux - Newbie 2 02-27-2005 05:13 PM
Exsists in Linux a command similar to ipconfig? zWaR Linux - Newbie 1 03-05-2004 05:27 PM
the diff command or any other similar things murshed Linux - Newbie 4 02-12-2003 10:07 PM

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

All times are GMT -5. The time now is 08:23 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
Open Source Consulting | Domain Registration