Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-19-2011, 04:43 PM   #1
LQ Newbie
Registered: Jul 2010
Posts: 19

Rep: Reputation: 0
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
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!
Old 02-19-2011, 05:00 PM   #2
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
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 You could either use:
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:
find . -name "*.java" -exec grep class {} \;
Old 02-19-2011, 07:20 PM   #3
LQ Newbie
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
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?
Old 02-19-2011, 09:01 PM   #4
Registered: Dec 2007
Distribution: cp6
Posts: 44

Rep: Reputation: 2
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.

Last edited by hogar.strashni; 02-19-2011 at 09:07 PM.
Old 02-20-2011, 11:39 AM   #5
LQ Newbie
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
thank you hogar, you confirmed what I suspected. thank you again!


grep, shell

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 not recognizing all the meta-characters when used with -e option. Rohit_4739 Linux - Newbie 3 10-14-2010 01:52 AM
Server Check Scripts with grep Option moyousif Linux - Newbie 6 11-27-2008 11:08 AM
understand -w option with grep geeyathink Programming 5 02-21-2008 04:17 AM
Paragraph-option to grep TheSpork Linux - General 2 03-17-2006 01:45 AM
grep option needed ziox Linux - General 7 01-04-2005 11:02 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration