LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with grep (or similar command) (http://www.linuxquestions.org/questions/linux-newbie-8/help-with-grep-or-similar-command-622783/)

tejama 02-21-2008 09:31 AM

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.

timnp 02-21-2008 09:35 AM

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

tejama 02-21-2008 09:37 AM

Thanks for the quick reply.

I'm actually looking to find text in the body of the file.

catbird 02-21-2008 10:05 AM

Quote:

Originally Posted by tejama (Post 3065010)
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

sycamorex 02-21-2008 10:34 AM

Try:

grep phrase_you_are_looking_for file

timnp 02-21-2008 10:52 AM

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?

tejama 02-21-2008 11:47 AM

Quote:

Originally Posted by sycamorex (Post 3065087)
grep phrase_you_are_looking_for file

Nope...this isn't what I was looking for at all.

sycamorex 02-21-2008 04:35 PM

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?

griffey 02-21-2008 04:47 PM

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.--

tejama 02-21-2008 10:28 PM

Quote:

Originally Posted by sycamorex (Post 3065446)
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.

frndrfoe 02-21-2008 11:46 PM

err..
Did you see the post from timnp?

Ashok_mittal 02-22-2008 01:09 AM

Quote:

Originally Posted by tejama (Post 3065704)
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

tejama 02-22-2008 07:32 AM

Quote:

Originally Posted by frndrfoe (Post 3065749)
err..
Did you see the post from timnp?

Sorry...somehow skipped over this timnp's post. Much thanks to you all (especially timnp!)


All times are GMT -5. The time now is 12:41 PM.