我可以在树中的所有文本文件(但不是二进制文件)中搜索某个字符串吗
到目前为止,我最好的办法是(在包含大型 C 程序的目录中查找字符串),
find ~/example_directory -type f \( -name "*.mk" -or -name "*.[sch]" \) -print0 | xargs -0 -e grep "example_string"
效果很好,但它依赖于 .mk makefile、.c 或 .h 源文件和 .s 中的所有有趣的东西汇编程序文件。
我正在考虑添加“所有名为 Makefile 的文件”或“所有 *.py python 脚本”之类的内容,但如果有某种方法告诉 find 只查找文本文件,事情会更容易。
如果您只是对所有文件运行 grep,则需要很长时间,并且您会在目标文件上得到许多无趣的命中。
My best shot so far is (for looking for strings in a directory containing a large C program)
find ~/example_directory -type f \( -name "*.mk" -or -name "*.[sch]" \) -print0 | xargs -0 -e grep "example_string"
Which works pretty well, but it relies on all the interesting things being in .mk makefiles, .c or .h source files, and .s assembler files.
I was thinking of adding in things like 'all files called Makefile' or 'all *.py python scripts', but it occurs that it would be way easier if there were some way to tell find only to find the text files.
If you just run grep on all files, it takes ages, and you get lots of uninteresting hits on object files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GNU grep 支持
-I
选项,这使得它将二进制文件(通过查看前几个字节确定)视为不匹配,因此基本上会跳过它们。GNU grep supports the
-I
option, which makes it treat binary files (as determined by looking at the first few bytes) as if they don't match, so they are essentially skipped.'-r' 开关使 grep 递归,'-I' 使其忽略二进制文件。
还有其他开关可以排除某些文件和目录(例如,我经常这样做来排除 svn 元数据)
The '-r' switch makes grep recurse, and '-I' makes it ignore binary files.
There are additional switches to exclude certain files and directories (I frequently do this to exclude svn metadata, for example)
您看过ack吗?
使用 ack 的十大理由:
Have you looked at ack?
From the top 10 reasons to use ack:
您可以使用
grep -I
忽略二进制文件。使用 GNU Parallel 而不是 xargs 将允许您将工作分解为多个进程,利用一些并行加速。文档中有一个如何执行并行 grep 的示例:
http://www.gnu.org/s/parallel/man.html#example__parallel_grep
You can use
grep -I
to ignore binary files. Using GNU Parallel instead of xargs will allow you to break up the work into multiple processes, exploiting some parallelism for speedup.There is an example of how to perform a parallel grep available in the documentation:
http://www.gnu.org/s/parallel/man.html#example__parallel_grep