Linux 如何从 grep -R 中排除目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6565471/
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
How can I exclude directories from grep -R?
提问by TIMEX
I want to traverse all subdirectories, except the "node_modules" directory.
我想遍历所有子目录,除了“node_modules”目录。
采纳答案by hornetbzz
SOLUTION 1 (combine findand grep)
解决方案 1(结合find和grep)
The purpose of this solution is not to deal with grepperformance but to show a portable solution : should also work with busybox or GNU version older than 2.5.
此解决方案的目的不是处理grep性能问题,而是展示一个可移植的解决方案:还应该与 busybox 或 2.5 之前的 GNU 版本一起使用。
Use find, for excluding directories foo and bar :
使用find, 排除目录 foo 和 bar :
find /dir \( -name foo -prune \) -o \( -name bar -prune \) -o -name "*.sh" -print
Then combine findand the non-recursive use of grep, as a portable solution :
然后结合find和非递归使用grep, 作为便携式解决方案:
find /dir \( -name node_modules -prune \) -o -name "*.sh" -exec grep --color -Hn "your text to find" {} 2>/dev/null \;
SOLUTION 2 (recursive use of grep):
解决方案2(递归使用grep):
You know this solution already, but I add it since it's the most recent and efficient solution. Note this is a less portable solution but more human-readable.
您已经知道此解决方案,但我添加了它,因为它是最新且最有效的解决方案。请注意,这是一种不太便携的解决方案,但更具人类可读性。
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
To exclude multiple directories, use --exclude-diras:
要排除多个目录,请使用--exclude-dir:
--exclude-dir={node_modules,dir1,dir2,dir3}
--exclude-dir={node_modules,dir1,dir2,dir3}
SOLUTION 3 (Ag)
解决方案 3 (Ag)
If you frequently search through code, Ag (The Silver Searcher)is a much faster alternative to grep, that's customized for searching code. For instance, it automatically ignores files and directories listed in .gitignore, so you don't have to keep passing the same cumbersome exclude options to grepor find.
如果您经常搜索代码,Ag(The Silver Searcher)是 grep 的更快替代品,它是为搜索代码而定制的。例如,它会自动忽略 中列出的文件和目录.gitignore,因此您不必一直将相同繁琐的排除选项传递给grep或find。
回答by DipSwitch
You could try something like grep -R search . | grep -v '^node_modules/.*'
你可以尝试类似的东西 grep -R search . | grep -v '^node_modules/.*'
回答by Hyman
find . ! -name "node_modules" -type d
回答by Johnsyweb
Recent versions of GNU Grep(>= 2.5.2) provide:
--exclude-dir=dir
which excludes directories matching the pattern dirfrom recursive directory searches.
它dir从递归目录搜索中排除与模式匹配的目录。
So you can do:
所以你可以这样做:
grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
For a bit more information regarding syntax and usage see
有关语法和用法的更多信息,请参阅
- The GNU man page for File and Directory Selection
- A related StackOverflow answer Use grep --exclude/--include syntax to not grep through certain files
- 文件和目录选择的 GNU 手册页
- 相关的 StackOverflow 答案使用 grep --exclude/--include 语法不通过某些文件进行 grep
For older GNU Greps and POSIX Grep, use findas suggested in other answers.
对于较旧的 GNU Greps 和POSIX Grep,请find按照其他答案中的建议使用。
Or just use ack(Edit: or The Silver Searcher) and be done with it!
回答by Morris
A simpler way would be to filter your results using "grep -v".
一种更简单的方法是使用“grep -v”过滤结果。
grep -i needle -R * | grep -v node_modules
grep -i needle -R * | grep -v node_modules
回答by Nestor Urquiza
Very useful, especially for those dealing with Node.jswhere we want to avoid searching inside "node_modules":
非常有用,特别是对于那些处理Node.js 的人,我们希望避免在“node_modules”中搜索:
find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
回答by Azodium
If you want to exclude multiple directories:
如果要排除多个目录:
"r" for recursive, "l" to print only names of files containing matches and "i" to ignore case distinctions :
"r" 表示递归,"l" 只打印包含匹配项的文件名,"i" 忽略大小写区别:
grep -rli --exclude-dir={dir1,dir2,dir3} keyword /path/to/search
Example : I want to find files that contain the word 'hello'. I want to search in all my linux directories exceptprocdirectory, bootdirectory, sysdirectory and rootdirectory :
示例:我想查找包含单词“hello”的文件。我想搜索除proc目录、引导目录、sys目录和根目录之外的所有 linux目录:
grep -rli --exclude-dir={proc,boot,root,sys} hello /
Note : The example above needs to be root
注意:上面的例子需要root
Note 2 (according to @skplunkerin) : do not add spaces after the commas in{dir1,dir2,dir3}
注2(根据@skplunkerin):不要在逗号后添加空格{dir1,dir2,dir3}
回答by arcseldon
Frequently use this:
经常使用这个:
grepcan be used in conjunction with -r(recursive), i(ignore case) and -o(prints only matching part of lines). To exclude filesuse --excludeand to exclude directories use --exclude-dir.
grep可以与-r(recursive), i(ignore case) and -o(prints only match part of lines) 结合使用。要排除files使用--exclude和排除目录,请使用--exclude-dir.
Putting it together you end up with something like:
把它放在一起,你最终会得到类似的东西:
grep -rio --exclude={filenames comma separated} \
--exclude-dir={directory names comma separated} <search term> <location>
Describing it makes it sound far more complicated than it actually is. Easier to illustrate with a simple example.
描述它使它听起来比实际复杂得多。用一个简单的例子更容易说明。
Example:
例子:
Suppose I am searching for current project for all places where I explicitly set the string value debuggerduring a debugging session, and now wish to review / remove.
假设我正在为我debugger在调试会话期间显式设置字符串值的所有位置搜索当前项目,现在希望查看/删除。
I write a script called findDebugger.shand use grepto find all occurrences. However:
我编写了一个名为findDebugger.sh并用于grep查找所有事件的脚本。然而:
For file exclusions - I wish to ensure that .eslintrcis ignored (this actually has a linting rule about debuggerso should be excluded). Likewise, I don't want my own script to be referenced in any results.
对于文件排除 - 我希望确保.eslintrc被忽略(这实际上有一个关于debugger因此应该被排除的 linting 规则)。同样,我不希望在任何结果中引用我自己的脚本。
For directory exclusions - I wish to exclude node_modulesas it contains lots of libraries that do reference debuggerand I am not interested in those results. Also I just wish to omit .ideaand .githidden directories because I don't care about those search locations either, and wish to keep the search performant.
对于目录排除 - 我希望排除node_modules它,因为它包含许多引用的库,debugger我对这些结果不感兴趣。此外,我只想省略.idea和.git隐藏目录,因为我也不关心这些搜索位置,并希望保持搜索性能。
So here is the result - I create a script called findDebugger.shwith:
所以这是结果 - 我创建了一个名为的脚本findDebugger.sh:
#!/usr/bin/env bash
grep -rio --exclude={.eslintrc,findDebugger.sh} \
--exclude-dir={node_modules,.idea,.git} debugger .
回答by Dung
A simple working command:
一个简单的工作命令:
root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org"
Above I grep for text "creativecommons.org" in current directory "dspace" and exclude dirs {log,assetstore}.
上面我在当前目录“dspace”中搜索文本“creativecommons.org”并排除目录{log,assetstore}。
Done.
完毕。
回答by angelo.mastro
This one works for me:
这个对我有用:
grep <stuff> -R --exclude-dir=<your_dir>

