我正在我的Windows-10 PC上开发一个Ubuntu应用程序,uname -a的结果如下:
Linux DOMINIQUEDS 4.4.0-17134-Microsoft #48-Microsoft Fri Apr 27 18:06:00 PST 2018 x86_64 x86_64 x86_64 GNU/Linux我正在进行一些C++开发,我想知道哪些源文件(*.cpp或*.h)包含了文件Sample.h,所以我启动了以下命令:
find ./ -name "*.cpp" -or -name "*.h" -exec grep -i "include" {} /dev/null \; | grep "Sample.h"这似乎行不通:只给出了*.h文件,其中包含了同一行的include和Sample.h。
但是,我确信,用于查找不同类型文件的-o结构是正确的:
find ./ -name "*.cpp" -or -name "*.h"=>这里,我得到了一个*.cpp和*.h文件的列表。
这给我留下了两种可能性:
-exec参数仅用于最后一个find结果。在这种情况下,有人能告诉我如何对所有的-exec结果执行find吗?提前感谢
发布于 2018-05-15 09:17:53
不过,我确信,用于查找不同类型文件的-o结构是正确的: find ./ -name "*.cpp“-or -name *.h
没错,但-or的优先级并不高。来自man find:
Please note that -a when specified implicitly (for example by two tests
appearing without an explicit operator between them) or explicitly has
higher precedence than -o. This means that find . -name afile -o -name
bfile -print will never print afile.所以:
-name "*.cpp" -or -name "*.h" -exec grep ...就像:
-name "*.cpp" -or ( -name "*.h" -exec grep ... )也不像:
( -name "*.cpp" -or -name "*.h" ) -exec grep ...你需要:
find . \( -name '*.cpp' -o -name '*.h' \) -exec grep -H '#include.*Sample\.h' {} +(我猜想您使用/dev/null来使grep打印文件名?) -H选项做到这一点。)
发布于 2018-05-15 10:36:10
用现代的grep,你根本不需要find。
grep -r --include='*.cpp' --include='*.h' 'include' . | grep 'Sample\.h'或者更好(考虑到搜索词的顺序是明确的)
grep -r --include='*.cpp' --include='*.h' 'include.*Sample\.h' .https://askubuntu.com/questions/1036433
复制相似问题