Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

How can I grep for a given string in all files in the current directory, and recursively so, only considering the first line? (the #! line, if present, but only if #! is in the -first- line)?

share|improve this question

marked as duplicate by Stéphane Chazelas, manatwork, jasonwryan, rahmu, Gilles Feb 28 '13 at 9:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

2 Answers 2

If you are not forced to use grep:

find . -type f -exec \
  awk 'FNR==1 && /GIVEN_STRING/ { print FILENAME ": " $0; }; FNR>1 {nextfile}' {} +

This causes a lot less CPU load as one process (or very few) does all the work.

share|improve this answer
1  
+1 for the proper solution =) –  sputnick Feb 28 '13 at 0:00
add comment

Try doing this :

find . -type f -exec head -1 {} \; | grep "GIVEN_STRING"
share|improve this answer
    
Two processes for every checked file? –  Hauke Laging Feb 27 '13 at 23:26
    
one process for every checked file, 2 processes to get everything set up. besides, spawning a head for each file isn't that big a deal. not with today's computers and kernels, anyway. –  Bananguin Feb 28 '13 at 7:34
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.