我怎样才能从grep -R排除目录?
我想遍历除“node_modules”目录外的所有子目录。
FIND排除目录foo和bar:
 find /dir \( -name foo -prune \) -o \( -name bar -prune \) -o -name "*.sh" -print 
  GREP -R: 
 你已经知道了 
结合FIND和GREP:
 find /dir \( -name node_modules -prune \) -o -name "*.sh" -exec grep --color -Hn "your text to find" {} 2>/dev/null \; 
银
 如果您经常search代码, Ag(银search器)是grep更快的替代scheme,它是为search代码而定制的。 例如,它会自动忽略在.gitignore列出的文件和目录,所以你不必保持grep或find的繁琐排除选项。 
GNU Grep (> = 2.5.2 )的最新版本提供:
 --exclude-dir=dir 
从recursion目录search中排除与模式目录匹配的目录。
所以你可以这样做:
 grep -R --exclude-dir=node_modules 'some pattern' /path/to/search 
有关语法和用法的更多信息,请参阅
- 文件和目录select的GNU手册页
- 相关的StackOverflow答案使用grep –exclude / – include语法不通过某些文件grep
 对于较老的GNU Greps和POSIX Grep ,使用其他答案中build议的find 。 
 或者只是使用ack ( 编辑 :或者Silver Searcher )并完成它! 
如果你想排除多个目录:
“r”表示recursion,“l”表示只打印包含匹配的文件名,“i”表示忽略大小写区分:
 grep -rli --exclude-dir = {dir1,dir2,dir3}关键字/path/ to / search
例如:我想查找包含单词“hello”的文件。 我想search除 proc目录, boot目录, sys目录和根目录以外的所有linux目录:
 grep -rli --exclude-dir = {proc,boot,root,sys} hello /
注意:上面的例子需要是root的
  注2(根据@skplunkerin):不要在 {dir1,dir2,dir3} 的逗号后加空格 
经常使用这个:
  grep可以与-r (recursion), i (忽略大小写)和-o (仅打印匹配的部分行)一起使用。 要排除files使用--exclude和排除目录使用--exclude-dir 。 
把它放在一起,你会得到如下结果:
 grep -rio --exclude={filenames comma separated} \ --exclude-dir={directory names comma separated} <search term> <location> 
描述它听起来比实际上要复杂得多。 用一个简单的例子更容易说明。
例:
 假设我正在为debugging会话期间显式设置string值debugger所有地方search当前项目,现在希望查看/删除。 
 我写了一个名为findDebugger.sh的脚本,并使用grep来查找所有的事件。 然而: 
 对于文件排除 – 我希望确保.eslintrc被忽略(这实际上有一个关于debugger的linting规则,所以应该被排除)。 同样,我不希望自己的脚本被任何结果引用。 
 对于目录排除 – 我希望排除node_modules因为它包含很多引用debugger的库,我对这些结果不感兴趣。 另外我只想省略.idea和.git隐藏的目录,因为我不关心那些search位置,并希望保持search的性能。 
 所以这就是结果 – 我创build了一个名为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/.*' 
非常有用,特别是那些处理Node.js的地方,我们要避免在“node_modules”里search:
 find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword 
一个简单的工作命令:
 root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org" 
上面我grep当前目录“dspace”中的文本“creativecommons.org”,并排除dirs {log,assetstore}。
完成。
 find . ! -name "node_modules" -type d 
这个为我工作
 grep <stuff> -R --exclude-dir=<your_dir> 
更简单的方法是使用“grep -v”过滤结果。
 grep -i needle -R * | grep -v node_modules