How to grep a String in all the java files in subdirectories?
24,137
Solution 1
Use recursive grep:
grep -r "String" --include=*.java .
Solution 2
I guess the unxUtils
provide the find
utility. So you could try to do:
find . -name "*.java" -exec grep "String" {} \;
or
find . -name "*.java" -exec grep "String" {} \+
The first version will execute one grep
command for each file found. The latter version will only execute as many grep
commands as necessary but not every find
version supports the +
argument.
Comments
-
Freewind over 3 years
I want to use
grep
to find the lines containString
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 byunxUtils
, which doesn't support--include
parameter. Is there any other way? -
Freewind almost 13 yearsthank you. I found it works on linux, but now I'm on windows. Is there any other way?
-
Freewind almost 13 yearsThe bad news is
find . -name "*.java"
doesn't work forfild
ofunxUtils
, it reportsFile not found - *.java
. Finally, I just installed thecygwin
-
anubhava almost 13 yearsIf you have cygwin installed same command will work on Windows as well, I have actually tried on my cygwin and it worked fine on Windows 7 as well.
-
gp-coder about 9 yearsIt might be helpful to add a filename as part of the search to make it easier. The print argument will help here:find . -name "*.java" -print -exec grep "String" {} \;
-
WesternGun about 5 years..or gitbash, as it has amounts of executable useful and full support of Windows path.