简体   繁体   English

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

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

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. 我正在尝试在当前文件夹中的所有文件中搜索字符串...

As Andy White said, you have to use fgrep in order to match for plain . 正如安迪怀特所说,你必须使用fgrep来匹配普通. , or escape the dots. ,或逃避点。

So you have to write ( -type f is 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 current directory and not its subdirs : 如果您只想要当前目录而不是其子目录:

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

'.' '' 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。)

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

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

If you are literally typing grep '...' you'll match just about any string. 如果你真的在输入grep '...'你就会匹配任何字符串。 I doubt you're actually typing '...' for your grep command, but if you are, the ... will match any three characters. 我怀疑你实际上是'...'为你的grep命令键入'...' ,但如果你是,那么...将匹配任意三个字符。

Please post more info on what you're searching for, and maybe someone can help you out more. 请发布有关您正在搜索的内容的更多信息,也许有人可以为您提供更多帮助。

If you're looking for a filename that matches, try: 如果您要查找匹配的文件名 ,请尝试:

find . -name "filename pattern" 

or 要么

find . | grep "filename pattern"

If your looking for looking for files that match (ie it contains the grep string) 如果您正在寻找匹配的文件 (即它包含grep字符串)

find . | xargs grep "string pattern"

works fine. 工作正常。 or simply: 或者干脆:

grep "string pattern" -R *

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 ! 我可以补充一下:RTFM! Or in a more polite way : use & abuse of 或者以更礼貌的方式:使用和滥用

man command

!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM