LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-28-2008, 01:10 PM   #1
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Rep: Reputation: 16
grep multiple files


Hi,
Can anybody tell me how to search through all the files in a directory, and return the file names of those that contain a certain string?
Thanks
 
Old 03-28-2008, 01:21 PM   #2
BlueC
Member
 
Registered: Aug 2007
Posts: 122

Rep: Reputation: 17
there are a lot of ways to do that. one would be:

ls -a | grep thestring

where thestring is the string you are searching for
 
Old 03-28-2008, 01:31 PM   #3
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Original Poster
Rep: Reputation: 16
hmm .. didn't seem to work. I should probably also mention that I am in a directory that starts with '.'
 
Old 03-28-2008, 01:37 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
It does not work because it greps the list of files, that is the file names... not the content of the file themselves. You can try something like
Code:
find . -type f -print0 | xargs -0 grep -l <pattern>
The -l option to grep is to print file names only (suppress the output of the matching lines).
 
Old 03-28-2008, 01:37 PM   #5
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Original Poster
Rep: Reputation: 16
Oh, I want any file that contains the string in it, not just in the filename. That may not have been clear as well..
Code:
$ cat theFile
this is a file
$ cat anotherFile
Look, another file!
$
I want 'theFile' to be returned if I look for 'a file'.
 
Old 03-28-2008, 01:39 PM   #6
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Original Poster
Rep: Reputation: 16
colucix beat me to my own correction. Good work.
 
Old 03-28-2008, 01:40 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
He he he... just a bunch of seconds!
 
Old 03-28-2008, 02:20 PM   #8
BlueC
Member
 
Registered: Aug 2007
Posts: 122

Rep: Reputation: 17
Quote:
Originally Posted by colucix View Post
It does not work because it greps the list of files, that is the file names... not the content of the file themselves.
Doh! I wasn't sure whether the OP wanted to search the content or the filename!

This would work to find "thestring" within files in the current directory (and all its subdirectories) and output the file name, the line number of the occurrence of the string, and the line itself:

Code:
find . -type f -exec grep -inH "thestring" {} \;
You could change that to search within all *.php files (for example):

Code:
find . -name \*.php -exec grep -inH "thestring" {} \;
HTH

Last edited by BlueC; 03-28-2008 at 02:21 PM.
 
Old 04-01-2008, 12:08 PM   #9
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Original Poster
Rep: Reputation: 16
Many thanks, works like a charm
Is the string case sensitive?
(By the looks of testing, it is not)

Last edited by Curtor; 04-01-2008 at 12:46 PM.
 
Old 04-01-2008, 12:54 PM   #10
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
If you are using GNU grep, you can use the -r flag. E.g.,
Code:
grep -lr thestring thedir
Where “thestring” is a regular expression and “thedir” is the name of the directory (which can be “.”).

If your regular expression is a fixed string, you can use the -F flag and if you want case-insensitive matching, you can use the -i flag.

Last edited by osor; 04-01-2008 at 12:55 PM.
 
Old 07-06-2008, 02:32 PM   #11
Cold-Gin
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Rep: Reputation: 0
Thanks, Colucix

Your find command was just what I needed.

Thanks again.
 
  


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
grep multiple strings GEEXTER Linux - General 7 12-06-2013 10:56 PM
grep for multiple patterns???? lucastic Linux - Software 4 08-06-2010 07:07 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
need to search (grep) multiple directories red_nexus Linux - Newbie 5 07-04-2006 11:44 PM
Using grep to change multiple files zwyrbla Linux - General 5 01-11-2005 03:14 PM


All times are GMT -5. The time now is 01:03 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