我想遍历所有子目录,除了“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 -v”来过滤结果。
grep -v node_modules . grep -i needle -R * |
如果你在git存储库中grep代码,而node_modules在你的.gitignore中,你可以使用git grep。Git grep在工作树中搜索被跟踪的文件,忽略来自.gitignore的所有文件
git grep "STUFF"
最新版本的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(编辑:或银色搜索者),并完成它!
find . ! -name "node_modules" -type d
非常有用,特别是对于那些处理Node.js时,我们希望避免在“node_modules”中搜索:
find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
推荐文章
- 使用sudo时未找到命令
- 当有命令行参数时,如何使用GDB分析程序的核心转储文件?
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何强制从另一个SSH会话分离屏幕?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- Linux Bash中双&和分号有什么区别?
- 在SSH会话中查找客户端的IP地址
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Linux删除大小为0的文件
- Spring引导应用程序作为服务
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- Windows和Linux上的c++编译:ifdef开关
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务