LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-15-2009, 05:09 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
How to grep a string from all the files under /


I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):

Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.

Is this command wrong?
 
Old 09-15-2009, 05:36 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by thomas2004ch View Post
I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):

Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.

Is this command wrong?
Yes. Try this:

Code:
path="/path/of/interest"
search="my string"

find $path -type f | while read filepath
do
   if grep -iqP "\b$search\b" "$filepath"
   then
      echo "String \"$search\" found in file \"$filepath\"."
   fi
done
 
Old 09-15-2009, 05:57 AM   #3
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
This should work all recent versions of bash.

Code:
grep -R "my text" *
 
Old 09-15-2009, 06:52 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by lutusp View Post
Yes. Try this:

Code:
path="/path/of/interest"
search="my string"

find $path -type f | while read filepath
do
   if grep -iqP "\b$search\b" "$filepath"
   then
      echo "String \"$search\" found in file \"$filepath\"."
   fi
done
I find your solution is a little bit too complicated.
 
Old 09-15-2009, 06:55 AM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by lutusp View Post
Yes. Try this:

Code:
path="/path/of/interest"
search="my string"

find $path -type f | while read filepath
do
   if grep -iqP "\b$search\b" "$filepath"
   then
      echo "String \"$search\" found in file \"$filepath\"."
   fi
done
This will be quite slow for a large number of hits as you're launching a new instance of grep for every file you find; you can just put the grep into the find command using the + instead of \; termination, then grep will be given a large number of files instead of only a single file:
Code:
find /path/of/interest -type f -exec grep "my string" "{}" +
Just a thought.
 
Old 09-15-2009, 08:03 AM   #6
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by rizwanrafique View Post
This should work all recent versions of bash.

Code:
grep -R "my text" *
I've tried and this is the same as "grep -r "my text" * what I used.
 
Old 09-15-2009, 08:32 AM   #7
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by pwc101 View Post
This will be quite slow for a large number of hits as you're launching a new instance of grep for every file you find; you can just put the grep into the find command using the + instead of \; termination, then grep will be given a large number of files instead of only a single file:
Code:
find /path/of/interest -type f -exec grep "my string" "{}" +
Just a thought.
Yes, you are right. It will take very very long to find out the files. I am not sure if there is a best way.

The problem by your solution is, one has to know the path.
 
Old 09-15-2009, 08:54 AM   #8
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by thomas2004ch View Post
Yes, you are right. It will take very very long to find out the files. I am not sure if there is a best way.
The "best way" does not exist. For anything. All you can di is find the approach which suits the problem you are trying to overcome with minimal negative side effects.
Quote:
Originally Posted by thomas2004ch View Post
The problem by your solution is, one has to know the path.
Not necessarily. You can supply a search path of /, or refine the other search criteria further, using mtime/atime/ctime, size etc.
 
Old 09-15-2009, 09:01 AM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
If the above methods were too complicated and/or didn't produce the results you want, you may not find this any better, but try:

Code:
for files in $(find / -name "*" -type f); do
 [ "$(grep -e "Volkerding" "$files")" ] && echo "$files"
done
..replace "Volkerding" with the string you seek.

Sasha
 
Old 09-15-2009, 09:12 AM   #10
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by thomas2004ch View Post
I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):

Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.

Is this command wrong?
Try using the following command:
Code:
grep -lir "your text" /

Last edited by quanta; 09-15-2009 at 09:14 AM.
 
Old 09-15-2009, 10:46 AM   #11
dwives
LQ Newbie
 
Registered: Sep 2009
Posts: 10

Rep: Reputation: 0
Obvious newbie question(or answer?)... Why couldn't you just use

ls / | grep -switch "string"

?
 
Old 09-15-2009, 10:51 AM   #12
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by dwives View Post
Obvious newbie question(or answer?)... Why couldn't you just use

ls / | grep -switch "string"

?
Because that would only pass a list of the directories immediately under / to grep which would then only output those that matched "string". Using find or a recursive option allows the full tree to be passed. For example, given the following directory structure:
Code:
/root
/root/subdir1
/root/subdir2
/root/subdir2/subsubdir1
/root/subdir3
/root/subdir3/subsubdir2
Your code would only pass the subdirs as arguments to the grep command. Using either find or -R passes the subdirs and the subsubdirs - the commands travel through the entire tree.
 
Old 09-15-2009, 11:06 AM   #13
dwives
LQ Newbie
 
Registered: Sep 2009
Posts: 10

Rep: Reputation: 0
Quote:
Originally Posted by pwc101 View Post
Because that would only pass a list of the directories immediately under / to grep which would then only output those that matched "string". Using find or a recursive option allows the full tree to be passed. For example, given the following directory structure:
Code:
/root
/root/subdir1
/root/subdir2
/root/subdir2/subsubdir1
/root/subdir3
/root/subdir3/subsubdir2
Your code would only pass the subdirs as arguments to the grep command. Using either find or -R passes the subdirs and the subsubdirs - the commands travel through the entire tree.
So it would work with the -R arguement?
 
Old 09-15-2009, 11:08 AM   #14
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by dwives View Post
So it would work with the -R arguement?
This is what the manpage has to say on the matter:
Quote:
Originally Posted by man grep
-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.
However, my experience is this is not so fool-proof as using find to get a list of files, then passing that list to grep separately. See above for my suggestion.
 
Old 09-15-2009, 11:08 AM   #15
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by dwives View Post
So it would work with the -R arguement?
Also, `ls` does not produce a full path of the file being examined, so while the -R option will cause recursion of `ls` itself, it will not produce helpful results, like a full path to the file wherein the string may have been found.

EDIT: And, as pwc101 hinted at above, ls can produce results that the next-in-pipe command has troubles with, producing weird errors.

Cheers
Sasha

Last edited by GrapefruiTgirl; 09-15-2009 at 11:09 AM.
 
  


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
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
read files from a folder and grep a string bhagirathi Programming 6 07-06-2009 06:27 AM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 06:29 AM
grep the exact string only ZAMO Linux - General 11 08-28-2008 05:08 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM

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

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