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


当前回答

如果你在git存储库中grep代码,而node_modules在你的.gitignore中,你可以使用git grep。Git grep在工作树中搜索被跟踪的文件,忽略来自.gitignore的所有文件

git grep "STUFF"

其他回答

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 >”

一个简单的工作命令:

root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org"

上面我grep文本“creativecommons.org”在当前目录“dspace”和排除dirs{日志,资产存储}。

完成了。