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


当前回答

这个对我很有用:

grep <stuff> -R --exclude-dir=<your_dir>

其他回答

最新版本的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(编辑:或银色搜索者),并完成它!

步骤1:

vim ~ / . bash_profile

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

步骤2:

源~ / . bash_profile

用法:

搜索”< string_to_be_searched >”

你可以试试grep -R搜索。| grep -v '^node_modules/.*'

一个简单的工作命令:

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

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

完成了。

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

git grep "STUFF"