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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:09:47  来源:igfitidea点击:

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

stringgrep

提问by Freewind

I want to use grepto find the lines contain Stringin 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 grepprovided by unxUtils, which doesn't support --includeparameter. 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 unxUtilsprovide the findutility. 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 grepcommand for each file found. The latter version will only execute as many grepcommands as necessary but not every findversion supports the +argument.

第一个版本将为grep找到的每个文件执行一个命令。后一个版本只会grep根据需要执行尽可能多的命令,但并非每个find版本都支持该+参数。