在当前目录下的多个工程中查找指定字符串,排除 node_modules dir2 等指定目录。
下面是三种可以实现的方法:
-
结合 find 命令和 grep 命令 -exec
find . \( -name node_modules -prune \) -o \( -name dir2 -prune \) -o -name "*.js" -exec grep --color -Hn "xxxx" {} 2>/dev/null \;
-
使用 grep 命令的
--exclude-dir
参数 (此方法特别慢)
grep -R --exclude-dir={node_modules,dir2} 'xxxx' .
注意:--exclude-dir={dir1,dir2}
dir 中间不能有空格。 -
Ag:the_silver_searcher
优点:快,自动将 .gitignore 里的文件忽略
https://stackoverflow.com/questions/6565471/how-can-i-exclude-directories-from-grep-r
网友评论