我想遍历所有子目录,除了“node_modules”目录。


当前回答

一个更简单的方法是使用“grep -v”来过滤结果。

grep -v node_modules . grep -i needle -R * |

其他回答

这个对我很有用:

grep <stuff> -R --exclude-dir=<your_dir>
find . ! -name "node_modules" -type d 

最新版本的GNU Grep(>= 2.5.2)提供:

--exclude-dir=dir

从递归目录搜索中排除匹配模式dir的目录。

所以你可以这样做:

grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

有关语法和用法的更多信息,请参阅

文件和目录选择的GNU手册页 使用grep——exclude/——include语法在某些文件中不使用grep

对于较旧的GNU Greps和POSIX Grep,请按照其他答案中的建议使用find。

或者只是使用ack(编辑:或银色搜索者),并完成它!

非常有用,特别是对于那些处理Node.js时,我们希望避免在“node_modules”中搜索:

find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword

步骤1:

vim ~ / . bash_profile

search() {
    grep -InH -r --exclude-dir=*build*  -e "$1" .
}

步骤2:

源~ / . bash_profile

用法:

搜索”< string_to_be_searched >”