string 如何在子目录中的所有java文件中grep一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6573633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep a String in all the java files in subdirectories?
提问by Freewind
I want to use grep
to find the lines contain String
in all java files under current directory.
我想用来grep
查找String
当前目录下所有java文件中包含的行。
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?
我在 Windows 上,使用grep
提供的unxUtils
,它不支持--include
参数。有没有其他办法?
回答by anubhava
Use recursive grep:
使用递归 grep:
grep -r "String" --include=*.java .
回答by bmk
I guess the unxUtils
provide the find
utility. So you could try to do:
我猜unxUtils
提供了find
实用程序。所以你可以尝试这样做:
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.
第一个版本将为grep
找到的每个文件执行一个命令。后一个版本只会grep
根据需要执行尽可能多的命令,但并非每个find
版本都支持该+
参数。