我想ncread在当前目录及其子目录下的每个.m文件中找到所有出现的内容.我使用以下命令:
grep -R --include="\.m" ncread .
Run Code Online (Sandbox Code Playgroud)
但是命令会返回nothings.grep的联机帮助页说:
--include=GLOB
Search only files whose basename matches GLOB
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?谢谢!
因为,例如,基本名称foobar.m是foobar.
像这样使用find和grep:
find -name "*.m" | xargs grep "ncread"
Run Code Online (Sandbox Code Playgroud)
或者如果您的文件名恰好有空格:
find -name "*.m" -print0 | xargs -0 grep "ncread"
Run Code Online (Sandbox Code Playgroud)
编辑:
这是如何用grep做的:
grep -R --include='*.m' ncread .
Run Code Online (Sandbox Code Playgroud)
glob不是regexp,因此你需要使用glob语法而不是正则表达式语法:( --include='*.m' 注意单引号:你想逃避glob以避免shell的扩展)
全局字符摘要:
* - any number of any characters
? - any single character
[abc] - single 'a', 'b' or 'c' character
\ - escaping, e.g. \* = single '*' character
Run Code Online (Sandbox Code Playgroud)
如果您需要更多详细信息,可以在这里开始阅读glob:关于glob的Wiki页面