LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using grep with -r option (http://www.linuxquestions.org/questions/linux-newbie-8/using-grep-with-r-option-863794/)

bibiki 02-19-2011 04:43 PM

using grep with -r option
 
I have on my desktop around five java files. I also created a java file in directory Viti, located in desktop.

Since all java files contain string "class", I practice my usage of grep with the following:

grep class *.java

and this works fine. However, when I type the following:

grep -r class *.java
or
rgrep class *.java

I expect to be shown the file that's located in my Viti directory, too, but it won't show it. Please tell me what I am missing.

Thank you!

Reuti 02-19-2011 05:00 PM

The shell will expand the *.java to all matches in the current working directory. And this doesn't include any directory (unless there is a directory like foobar.java). You could either use:
Code:

grep -r class *
but it will scan then all files in all directories found in the current working directory. It could be combined with find though:
Code:

find . -name "*.java" -exec grep class {} \;

bibiki 02-19-2011 07:20 PM

would you be kind to tell what is your second way of doing that job actually doing?

is it like find finds all files that end with java? if so, then -exec... is unnecessary since all java files necessarily contain word class in them? am i wrong?

hogar.strashni 02-19-2011 09:01 PM

you are right - 'find .' finds all files in the current directory subtree whose name is ending with '.java'
you said you were searching for a word 'class' in *.java files, so @Reuti wrote you how to do that. If you were searching for a word 'class' only to display .java files, '-exec grep ...' would have been redundant.
@Reuti sorry for interfering. Hope you don't mind.

bibiki 02-20-2011 11:39 AM

thank you hogar, you confirmed what I suspected. thank you again!


All times are GMT -5. The time now is 04:40 PM.