LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   grep search... (http://www.linuxquestions.org/questions/linux-general-1/grep-search-496364/)

tobiasw 10-28-2006 10:17 AM

grep search...
 
Hi,
is it possible to search with grep for a text pattern, the whole root /?

something like:
grep -i 'test' * or /

thanks


Tobias

druuna 10-28-2006 10:32 AM

Hi,

I'm not sure which of the 2 you want:

If you only want to check all the files in /:

grep -i 'test' /*

If you need to do this recursive (/ and all the subdirs):

find / -type f -exec grep -i 'test' {} \;

The last command is very (!!) resource unfriendly and will probably run for a while. It would be better to narrow down the field it needs to search.

tobiasw 10-28-2006 10:47 AM

thanks druuna...

can I narrow it down with : find /home/test -type f -exec grep ... ?

druuna 10-28-2006 10:57 AM

Hi,

Yes you can. There are 2 possibilities:

cd /home/test
find . <whatever>


The . (dot) means: from the current directory.

or

find /home/test <whatever>

The second one can be executed from any point.

matthewg42 10-28-2006 01:36 PM

GNU grep supports the -r option, which means "recursive". It's a little less cumbersome than using find. e.g. you can grep files in sub-directories specified on the command line, e.g.

Code:

grep -r 'mypattern' /home/matthew
Will search all files in sub-directories of /home/matthew for "mypattern". You can search all files on the whole system using / instead of /home/matthew. Note that searching your whole filesystem probably won't be possible unless you run the command as root since many files are not readable by regular users.

Also note that it might be a good idea to run the command with nice since it'll take an age anyway, and you will probably want to minimize impact on other programs as it runs.


All times are GMT -5. The time now is 06:45 AM.