问题描述
我需要在 Linux 系统上找到filename.ext"的所有实例,并查看哪些包含文本lookingfor".
I need to find all instances of 'filename.ext' on a linux system and see which ones contain the text 'lookingfor'.
是否有一组可行的 linux 命令行操作?
Is there a set of linux command line operations that would work?
推荐答案
find / -type f -name filename.ext -exec grep -l 'lookingfor' {} +
使用 +
终止命令比 ;
更有效率,因为 find
将整批文件发送到 grep
而不是一一发送.这避免了对找到的每个文件的 fork/exec.
Using a +
to terminate the command is more efficient than ;
because find
sends a whole batch of files to grep
instead of sending them one by one. This avoids a fork/exec for each single file which is found.
不久前我做了一些测试来比较 xargs
与 {} +
与 {} ;
的性能,我发现{} +
更快.以下是我的一些结果:
A while ago I did some testing to compare the performance of xargs
vs {} +
vs {} ;
and I found that {} +
was faster. Here are some of my results:
time find . -name "*20090430*" -exec touch {} +
real 0m31.98s
user 0m0.06s
sys 0m0.49s
time find . -name "*20090430*" | xargs touch
real 1m8.81s
user 0m0.13s
sys 0m1.07s
time find . -name "*20090430*" -exec touch {} ;
real 1m42.53s
user 0m0.17s
sys 0m2.42s
这篇关于在 linux 系统上找到所有匹配 'name' 的文件,并用它们搜索 'text'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!