有三个需求(见标题), 而且这几个需求很常见
但是我搜索man文档,也按照它的指示做了,但是总感觉姿势不对(不符合预期结果)
然后Google了一番,结果无非和man文档差不多
所以我的问题可以转化为: 怎么读懂文档中关于exclude的部分?
根据我自己的理解,总结为以下几点:
使用shell通配符
--exclude-from, --exclude-dir分别针对文件,文件夹. --exclude貌似是一个大的选项
针对二进制文件用-I
, 但是grep怎么知道哪些是二进制文件? (应该有某种配置选项?)
比如有这几个情况,我构造的命令分别如下
忽略.pyc
文件 --exclude-from="*.pyc"
忽略.svn
目录 --exclude-dir="\.svn"
忽略二进制文件, -I
(我对这个选项真是不理解)
Linux Ubuntu16
GNU grep 2.6.3
我的alias: alias grep='egrep --color=auto'
我工作时构造的命令: grep -r --exclude-dir="\.svn" 'KeyValue\b' ./
`
man grep
有.svn目录最好, .git目录也可以, 或其他
尝试使用grep的exclude的几个选项
查看预期结果
摘抄grep的man文档中关于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. --exclude-from=FILE Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude). --exclude-dir=DIR Exclude directories matching the pattern DIR from recursive searches.
我构造的命令是grep -r --exclude-dir="\.svn" 'KeyValue\b' ./
报错信息为: 仍然会搜索.svn中的文件
结果很好找,但是不知道为何我的结果有点不同
Google了
grep ignore spcific directory
grep exclude binary file
grep exclude some file
grep exclude pyc file
去掉 \
grep -r --exclude-dir=".svn" 'KeyValue\b' ./
--exclude-dir
里的路径应该使用通配符(wildcard),文本查找使用正则表达式(regular expression)。
用管道,比如这样?
find -type f (-not -name '*ext') -and (-not -type d -name 'dir') | xargs -n1 -I{} grep --exclude=GLOB 'search_str' {}