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


当前回答

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

其他回答

这个语法

--exclude-dir={dir1,dir2}

被shell(例如Bash)而不是grep扩展为:

--exclude-dir=dir1 --exclude-dir=dir2

引用将阻止shell扩展它,所以这将不起作用:

--exclude-dir='{dir1,dir2}'    <-- this won't work

与——exclude-dir一起使用的模式与——exclude选项手册页中描述的模式相同:

--exclude=GLOB
    Skip files whose base name matches GLOB (using wildcard matching).
    A file-name glob can use *, ?, and [...]  as wildcards, and \ to
    quote a wildcard or backslash character literally.

shell通常会尝试自己展开这样的模式,所以为了避免这种情况,你应该引用它:

--exclude-dir='dir?'

你可以像这样一起使用大括号和引号排除模式:

--exclude-dir={'dir?','dir??'}

这个对我很有用:

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

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

git grep "STUFF"

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

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

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