linux - 如何从grep -R中排除目录?
我想遍历所有子目录,除了“node_modules”目录。
TIMEX asked 2018-12-31T01:18:05Z
12个解决方案
856 votes
最新版本的GNU Grep(> = 2.5.2)提供:
--exclude-dir=dir
从递归目录搜索中排除与模式ack匹配的目录。
所以你可以这样做:
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
有关语法和用法的更多信息,请参阅
文件和目录选择的GNU手册页
相关的StackOverflow答案使用grep --exclude / - include语法不要浏览某些文件
对于较旧的GNU Greps和POSIX Grep,请按照其他答案中的建议使用ack。
或者只使用ack(编辑:或银色搜索者)并完成它!
Johnsyweb answered 2018-12-31T01:20:07Z
161 votes
解决方案1(结合.gitignore和grep)
此解决方案的目的不是为了处理.gitignore性能,而是为了展示一个可移植的解决方案:还应该使用busybox或早于2.5的GNU版本。
使用.gitignore,排除目录foo和bar:
find /dir \( -name foo -prune \) -o \( -name bar -prune \) -o -name "*.sh" -print
然后结合.gitignore和非递归使用.gitignore,作为便携式解决方案:
find /dir \( -name node_modules -prune \) -o -name "*.sh" -exec grep --color -Hn "your text to find" {} 2>/dev/null \;
解决方案2(递归使用.gitignore):
您已经知道这个解决方案,但我添加它,因为它是最新且最有效的解决方案。 请注意,这是一种不太便携的解决方案,但更易于阅读。
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
解决方案3(Ag)
如果你经常搜索代码,Ag(The Silver Searcher)是一个比grep快得多的替代品,它是为搜索代码而定制的。 例如,它会自动忽略.gitignore中列出的文件和目录,因此您不必将相同的繁琐排除选项传递给grep或find。
hornetbzz answered 2018-12-31T01:18:59Z
61 votes
如果要排除多个目录:
“r”表示递归,“l”只打印包含匹配的文件名,“i”表示忽略大小写区别:
grep -rli --exclude-dir={dir1,dir2,dir3} keyword /path/to/search
示例:我想查找包含“hello”一词的文件。 我想搜索除proc目录,启动目录,sys目录和根目录之外的所有linux目录:
grep -rli --exclude-dir={proc,boot,root,sys} hello /
注意:上面的示例需要是root
注2(根据@skplunkerin):请勿在{dir1,dir2,dir3}中的逗号后添加空格
Azodium answered 2018-12-31T01:20:56Z
19 votes
这个语法
--exclude-dir={dir1,dir2}
由shell(例如Bash)而不是topdir/something/else扩展为:
--exclude-dir=dir1 --exclude-dir=dir2
引用会阻止shell扩展它,所以这不起作用:
--exclude-dir='{dir1,dir2}'
与topdir/something/else一起使用的模式与--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??'}
模式可以跨越多个路径段:
--exclude-dir='some*/?lse'
这将排除像topdir/something/else这样的目录。
Derek Veit answered 2018-12-31T01:22:03Z
13 votes
经常使用这个:
findDebugger.sh可与debugger(递归),.idea(忽略大小写)和.git(打印仅匹配部分行)一起使用。 要排除files使用--exclude并排除目录使用--exclude-dir。
把它放在一起你会得到类似的东西:
grep -rio --exclude={filenames comma separated} \
--exclude-dir={directory names comma separated}
描述它使它听起来比实际复杂得多。 用一个简单的例子更容易说明。
例:
假设我在调试会话期间显式设置字符串值findDebugger.sh的所有地方搜索当前项目,现在希望查看/删除。
我写了一个名为findDebugger.sh的脚本,并使用debugger查找所有事件。 然而:
对于文件排除 - 我希望确保忽略findDebugger.sh(这实际上有一个约为debugger的linting规则,因此应排除)。 同样,我不希望在任何结果中引用我自己的脚本。
对于目录排除 - 我希望排除findDebugger.sh,因为它包含许多参考debugger的库,我对这些结果不感兴趣。 另外我只想省略.idea和.git隐藏目录,因为我不关心那些搜索位置,并希望保持搜索性能。
所以这是结果 - 我创建了一个名为findDebugger.sh的脚本:
#!/usr/bin/env bash
grep -rio --exclude={.eslintrc,findDebugger.sh} \
--exclude-dir={node_modules,.idea,.git} debugger .
arcseldon answered 2018-12-31T01:23:20Z
9 votes
你可以尝试像grep -R search . | grep -v '^node_modules/.*'这样的东西
DipSwitch answered 2018-12-31T01:23:41Z
4 votes
非常有用,特别是对于那些我们想要避免在“node_modules”内搜索的Node.js的人:
find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
Nestor Urquiza answered 2018-12-31T01:24:04Z
2 votes
这个适合我
grep -R --exclude-dir=
angelo.mastro answered 2018-12-31T01:24:32Z
2 votes
一个简单的工作命令:
root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org"
上面我在当前目录“dspace”中找到文本“creativecommons.org”并排除dirs {log,assetstore}。
完成。
Dung answered 2018-12-31T01:25:07Z
1 votes
find . ! -name "node_modules" -type d
Jack answered 2018-12-31T01:25:22Z
1 votes
如果您正在使用git存储库中的代码并且.gitignore中有node_modules,则可以使用git grep。git grep搜索工作树中的跟踪文件,忽略.gitignore中的所有内容
git grep "STUFF"
0xcaff answered 2018-12-31T01:25:44Z
0 votes
更简单的方法是使用“grep -v”过滤结果。
grep -i needle -R * | grep -v node_modules
Morris answered 2018-12-31T01:26:13Z