Linux 使用 grep 搜索 find 提供的文件: find 有什么问题。| xargs grep '...'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1204781/
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-08-03 17:34:37  来源:igfitidea点击:

Using grep to search files provided by find: what is wrong with find . | xargs grep '...'?

regexlinuxconsolefindcommand

提问by cupakob

When I use the command:

当我使用命令时:

find . | xargs grep '...'

I get the wrong matches. I'm trying to search for the string ...in all files in the current folder.

我得到了错误的匹配。我正在尝试...在当前文件夹中的所有文件中搜索该字符串。

采纳答案by Steve Schnepp

As Andy White said, you have to use fgrepin order to match for plain ., or escape the dots.

正如安迪怀特所说,你必须使用fgrep才能匹配普通.,或者逃避点。

So you have to write (-type fis to only have the files : you obviously don't want the directories.) :

所以你必须写(-type f就是只有文件:你显然不想要目录。):

find . -type f | xargs fgrep '...'

or if you still want to use grep :

或者如果您仍然想使用 grep :

find . -type f | xargs grep '\.\.\.'

And if you only want the currentdirectory and not its subdirs :

如果你只想要当前目录而不是它的子目录:

find . -maxdepth 1 -type f | xargs fgrep '...'

回答by Andy White

If you are literally typing grep '...'you'll match just about any string. I doubt you're actually typing '...'for your grep command, but if you are, the ...will match any three characters.

如果您是从字面上打字,则几乎grep '...'可以匹配任何字符串。我怀疑您实际上是在'...'为您的 grep 命令打字,但如果您是,...它将匹配任何三个字符。

Please post more info on what you're searching for, and maybe someone can help you out more.

请发布有关您要搜索的内容的更多信息,也许有人可以为您提供更多帮助。

回答by Jeremy Powell

If you're looking for a filenamethat matches, try:

如果您正在寻找匹配的文件名,请尝试:

find . -name "filename pattern" 

or

或者

find . | grep "filename pattern"

If your looking for looking for filesthat match (ie it contains the grep string)

如果您正在寻找匹配的文件(即它包含 grep 字符串)

find . | xargs grep "string pattern"

works fine. or simply:

工作正常。或者干脆:

grep "string pattern" -R *

回答by Laurence Gonsalves

'.' matches any character, so you'll be finding all lines that contain 3 or more characters.

'.' 匹配任何字符,因此您将找到包含 3 个或更多字符的所有行。

You can either escape the dots, like this:

你可以逃避这些点,像这样:

find . | xargs grep '\.\.\.'

Or you can use fgrep, which does a literal match instead of a regex match:

或者您可以使用 fgrep,它执行文字匹配而不是正则表达式匹配:

find . | xargs fgrep '...'

(Some versions of grep also accept a -F flag which makes them behave like fgrep.)

(某些版本的 grep 还接受 -F 标志,这使它们的行为类似于 fgrep。)

回答by Isaac Clarke

To complete Jeremy's answer, you may also want to try

要完成 Jeremy 的回答,您可能还想尝试

find . -type f | xargs grep 'your_pattern'

or

或者

find . -type f -exec grep 'your_pattern' {} +

Which is similar to a xargs

这类似于 xargs

I might add : RTFM ! Or in a more polite way : use & abuse of

我可能会补充:RTFM!或者以更礼貌的方式:使用和滥用

man command

!

回答by ghostdog74

@OP, if you are looking for files that contain ...,

@OP,如果您正在寻找包含...的文件,

grep -R "\.\.\." *