Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep a String in all the java files in subdirectories?

Tags:

string

regex

grep

I want to use grep to find the lines contain String in all java files under current directory.

I tried:

grep -r "String" **/*.java 

But it doesn't work:

grep : **/*.java:  no such file or directory 

How to write it?


UPDATE

I'm on windows, using the grep provided by unxUtils, which doesn't support --include parameter. Is there any other way?

like image 349
Freewind Avatar asked Jul 04 '11 15:07

Freewind


People also ask

How do you grep a string from all subdirectories?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How can I recursively grep through subdirectories?

Grep command is used to search text from files. It is a versatile pattern that invokes grep with –r. –R option search files recursively from subdirectories, starting from the current directory. The command is run from the top-level directory.

How do you find a word in all files in a directory?

You can use grep tool to search recursively the current folder, like: grep -r "class foo" .


1 Answers

Use recursive grep:

grep -r "String" --include=*.java . 
like image 187
anubhava Avatar answered Oct 12 '22 05:10

anubhava