网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
./:ignore-case
./:all * .txt:文本文件(用\文件转义,以防你在文件名中有一个带星号的目录)
./:搜索什么
./:从当前目录开始。
HoldOffHunger answered 2018-12-23T02:14:01Z
44 votes
怎么样:
find . -name ‘*.h’ -o -name ‘*.cpp’ -exec grep “CP_Image” {} \; -print
Amir Afghani answered 2018-12-23T02:14:21Z
40 votes
grep -rnw “some thing to grep” --include=*.{module,inc,php,js,css,html,htm} ./
Yuri Malov answered 2018-12-23T02:14:36Z
13 votes
HP和Sun服务器上没有-r选项,这种方式适用于我的HP服务器
find . -name “*.c” | xargs grep -i “my great text”
-i用于不区分大小写的字符串搜索
Sam B answered 2018-12-23T02:15:01Z
8 votes
由于这是找文件的问题,让我们使用find!
使用GNU find,您可以使用grep选项在扩展名为–include或.cpp的目录树中查找这些文件:
find -type f -regex “.*\.\(h\|cpp\)”
# ^^^^^^^^^^^^^^^^^^^^^^^
然后,只需在每个结果上执行grep:
find -type f -regex “.*\.\(h\|cpp\)” -exec grep “your pattern” {} +
如果你没有这种发现分布,你必须使用像Amir Afghani这样的方法,使用grep连接选项(名称以grep或–include结尾):
find -type f \( -name ‘*.h’ -o -name ‘*.cpp’ \) -exec grep “your pattern” {} +
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如果您真的想使用grep,请按照指示的语法–include:
grep “your pattern” -r --include=*.{cpp,h}
# ^^^^^^^^^^^^^^^^^^^
fedorqui answered 2018-12-23T02:15:39Z
6 votes
最简单的方法是
find . -type f -name ‘*.extension’ | xargs grep -i string
user3640130 answered 2018-12-23T02:15:59Z
3 votes
我知道这个问题有点陈旧,但我想分享我通常用来查找.c和.h文件的方法:
tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H “#include”
或者如果您还需要行号:
tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH “#include”
Tom answered 2018-12-23T02:16:24Z
2 votes
ag(银色搜索者)有相当简单的语法
-G --file-search-regex PATTERN
Only search files whose names match PATTERN.
所以
ag -G *.h -G *.cpp CP_Image
Eldamir answered 2018-12-23T02:16:48Z
1 votes
以下答案很好。
grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com
但可以更新为:
grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP email@domain.com
哪个可以更简单。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!