views:

135

answers:

2

hi. maybe it's a bit strange - and maybe there are other tools to do this but, well..

i am using the following classic bash command to find all files which contain some string:

find . -type f | xargs grep "something"

i have a great number of files, on multiple depths. first occurence of "something" is enough for me, but find continues searching, and takes a long time to complete the rest of the files. what i would like to do is something like a "feedback" from grep back to find so that find could stop searching for more files. is such a thing possible?

thank you

+2  A: 

Simply keep it within the realm of find:

find . -type f -exec grep "something" {} \; -quit

This is how it works:

The -exec will work when the -type f will be true. And because grep returns 0 (success/true) when the -exec grep "something" has a match, the -quit will be triggered.

Chen Levy
+2  A: 
find -type f | xargs grep e | head -1

does exactly that: when the head terminates, the middle element of the pipe is notified with a 'broken pipe' signal, terminates in turn, and notifies the find. You should see a notice such as

xargs: grep: terminated by signal 13

which confirms this.

Kilian Foth