[英]How can I exclude directories from grep -R?
我想遍历所有子目录,除了“node_modules”目录。
--exclude-dir=dir
从递归目录搜索中排除与模式dir
匹配的目录。
所以你可以这样做:
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
有关语法和用法的更多信息,请参阅
对于较旧的 GNU Greps 和POSIX Grep ,请按照其他答案中的建议使用find
。
或者只是使用ack
( Edit : 或The Silver Searcher ) 并完成它!
解决方案 1(结合find
和grep
)
此解决方案的目的不是处理grep
的性能,而是展示一个可移植的解决方案:也应该适用于 2.5 之前的busybox 或 GNU 版本。
使用find
来排除目录 foo 和 bar:
find /dir \( -name foo -prune \) -o \( -name bar -prune \) -o -name "*.sh" -print
然后结合find
和grep
的非递归使用,作为一个可移植的解决方案:
find /dir \( -name node_modules -prune \) -o -name "*.sh" -exec grep --color -Hn "your text to find" {} 2>/dev/null \;
解决方案 2(使用grep
的--exclude-dir
选项):
您已经知道此解决方案,但我添加它是因为它是最新且有效的解决方案。 请注意,这是一个不太便携的解决方案,但更易于阅读。
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
要排除多个目录,请使用--exclude-dir
作为:
--exclude-dir={node_modules,dir1,dir2,dir3}
溶液 3(银)
如果您经常搜索代码, Ag (The Silver Searcher)是 grep 更快的替代品,它是为搜索代码而定制的。 例如,它会自动忽略.gitignore
中列出的文件和目录,因此您不必将相同的繁琐排除选项传递给grep
或find
。
如果要排除多个目录:
"r" 表示递归,"l" 仅打印包含匹配项的文件名,"i" 忽略大小写区别:
grep -rli --exclude-dir={dir1,dir2,dir3} keyword /path/to/search
示例:我想查找包含单词“hello”的文件。 我想在我所有的 linux 目录中搜索除proc目录、引导目录、 sys目录和根目录:
grep -rli --exclude-dir={proc,boot,root,sys} hello /
注意:上面的例子需要root
注意2(根据@skplunkerin):不要在{dir1,dir2,dir3}
中的逗号后添加空格
这种语法
--exclude-dir={dir1,dir2}
由 shell (例如 Bash)而不是grep
为:
--exclude-dir=dir1 --exclude-dir=dir2
引用将阻止 shell 扩展它,所以这不起作用:
--exclude-dir='{dir1,dir2}' <-- this won't work
--exclude-dir
使用的模式与--exclude
选项的手册页中描述的模式相同:
--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard matching).
A file-name glob can use *, ?, and [...] as wildcards, and \ to
quote a wildcard or backslash character literally.
shell 通常会尝试扩展这种模式本身,因此为避免这种情况,您应该引用它:
--exclude-dir='dir?'
您可以像这样一起使用花括号和带引号的排除模式:
--exclude-dir={'dir?','dir??'}
如果您正在查找 git 存储库中的代码并且node_modules
在您的.gitignore
中,则可以使用git grep
。 git grep
在工作树中搜索跟踪的文件,忽略.gitignore
中的所有内容
git grep "STUFF"
经常使用这个:
grep
可以与-r
(递归)、 i
(忽略大小写)和-o
(仅打印匹配的部分行)结合使用。 要排除files
使用--exclude
并排除目录使用--exclude-dir
。
把它放在一起,你最终会得到类似的东西:
grep -rio --exclude={filenames comma separated} \
--exclude-dir={directory names comma separated} <search term> <location>
描述它会使它听起来比实际上要复杂得多。 用一个简单的例子更容易说明。
例子:
假设我正在为所有在调试 session 期间明确设置字符串值debugger
的地方搜索当前项目,现在希望查看/删除。
我编写了一个名为findDebugger.sh
的脚本,并使用grep
来查找所有事件。 然而:
对于文件排除 - 我希望确保.eslintrc
被忽略(这实际上有一个关于debugger
的 linting 规则,所以应该被排除)。 同样,我不希望在任何结果中引用我自己的脚本。
对于目录排除 - 我希望排除node_modules
因为它包含许多引用debugger
的库,我对这些结果不感兴趣。 此外,我只想省略.idea
和.git
隐藏目录,因为我也不关心这些搜索位置,并希望保持搜索性能。
所以这是结果 - 我创建了一个名为findDebugger.sh
的脚本:
#!/usr/bin/env bash
grep -rio --exclude={.eslintrc,findDebugger.sh} \
--exclude-dir={node_modules,.idea,.git} debugger .
您可以尝试类似grep -R search. | grep -v '^node_modules/.*'
grep -R search. | grep -v '^node_modules/.*'
这里给出了许多正确的答案,但我添加这个是为了强调一个导致一些匆忙尝试失败的观点: exclude-dir
采用模式,而不是目录的路径。
假设您的搜索是:
grep -r myobject
你注意到你的 output 被src/other/objects-folder
的结果弄乱了。 此命令不会给您预期的结果:
grep -r myobject --exclude-dir=src/other/objects-folder
您可能想知道为什么exclude-dir
不起作用! 要实际从objects-folder
中排除结果,只需执行以下操作:
grep -r myobject --exclude-dir=objects-folder
换句话说,只使用文件夹名称,而不是路径。 一旦你知道它就很明显了。
从手册页:
--排除目录=GLOB
跳过名称后缀与模式 GLOB 匹配的任何命令行目录。 递归搜索时,跳过基本名称与 GLOB 匹配的任何子目录。 忽略 GLOB 中任何多余的尾部斜杠。
非常有用,特别是对于那些处理Node.js的人,我们希望避免在“node_modules”中搜索:
find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
一个简单的工作命令:
root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org"
以上 I grep 用于当前目录“dspace”中的文本“creativecommons.org”并排除目录 {log,assetstore}。
完毕。
这个对我有用:
grep <stuff> -R --exclude-dir=<your_dir>
步骤1:
vim ~/.bash_profile
search() {
grep -InH -r --exclude-dir=*build* -e "$1" .
}
第2步:
source ~/.bash_profile
用法:
search "<string_to_be_searched>"
find . ! -name "node_modules" -type d
更简单的方法是使用“grep -v”过滤结果。
grep -i needle -R * | grep -v node_modules
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.