3

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)?

0

2 Answers 2

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.

0
0

Try doing this :

find . -type f -exec head -1 {} \; | grep "GIVEN_STRING"
2
  • Two processes for every checked file? Commented Feb 27, 2013 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
    Commented Feb 28, 2013 at 7:34

Not the answer you're looking for? Browse other questions tagged .