LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to grep a string from all the files under / (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-grep-a-string-from-all-the-files-under-755309/)

thomas2004ch 09-15-2009 05:09 AM

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?

lutusp 09-15-2009 05:36 AM

Quote:

Originally Posted by thomas2004ch (Post 3683381)
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


rizwanrafique 09-15-2009 05:57 AM

This should work all recent versions of bash.

Code:

grep -R "my text" *

thomas2004ch 09-15-2009 06:52 AM

Quote:

Originally Posted by lutusp (Post 3683399)
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. ;)

pwc101 09-15-2009 06:55 AM

Quote:

Originally Posted by lutusp (Post 3683399)
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.

thomas2004ch 09-15-2009 08:03 AM

Quote:

Originally Posted by rizwanrafique (Post 3683420)
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.

thomas2004ch 09-15-2009 08:32 AM

Quote:

Originally Posted by pwc101 (Post 3683476)
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.

pwc101 09-15-2009 08:54 AM

Quote:

Originally Posted by thomas2004ch (Post 3683584)
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 (Post 3683584)
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.

GrapefruiTgirl 09-15-2009 09:01 AM

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

quanta 09-15-2009 09:12 AM

Quote:

Originally Posted by thomas2004ch (Post 3683381)
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" /

dwives 09-15-2009 10:46 AM

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

ls / | grep -switch "string"

?

pwc101 09-15-2009 10:51 AM

Quote:

Originally Posted by dwives (Post 3683754)
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.

dwives 09-15-2009 11:06 AM

Quote:

Originally Posted by pwc101 (Post 3683761)
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?

pwc101 09-15-2009 11:08 AM

Quote:

Originally Posted by dwives (Post 3683788)
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.

GrapefruiTgirl 09-15-2009 11:08 AM

Quote:

Originally Posted by dwives (Post 3683788)
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


All times are GMT -5. The time now is 03:48 PM.