Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

command line - Find all files matching 'name' on linux system, and search with them for 'text'

I need to find all instances of 'filename.ext' on a linux system and see which ones contain the text 'lookingfor'.

Is there a set of linux command line operations that would work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
find / -type f -name filename.ext -exec grep -l 'lookingfor' {} +

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...