I need to search the first 50 lines of every file in a directory and its subdirectories.
This will do the recursive part, but how do I limit to just the first 50 lines of each file?
grep -r "matching string here" .
Some of these files are huge, and I only want them to match in the first 50 lines. I'm trying to speed up the process by not searching megabytes of binary data in some files.
Best Answer
If you just want the files that match:
or
If you only want the matching strings:
or, better,
And if you want both:
Remarks.
sed
instead of the combohead
--grep
.-exec ... +
in each method, but then you'll have to code your inner loop yourself! (trivial exercise left to the reader). This might be very slightly more efficient if you have a gazillion files.