Grep Guru

Bill Campbell freebsd at
Sun Jun 8 23:07:16 UTC 2008

On Mon, Jun 09, 2008, Raphael Becker wrote:
>On Sun, Jun 08, 2008 at 10:15:50PM +0200, Wojciech Puchar wrote:
>> find . -type f -print0|xargs -0 grep <grepoptions> <text to search>
>There's no more need for find | xargs
>find . -type -f -exec grep <grepoptions> <text to search> {} \+
>-exec foo {} \+ behaves like xargs foo  
>-exec foo {} \; exec foo for every file

The issue here is that grep execs grep for each file found while
xargs batches the files.

This is of particular importance if one wants to see the file
names in the output.  In relation to this, if one wants to be
sure that grep always generates the file name, insure that it
always gets at least two files as arguments:

find . -type f -print0 | xargs -0 grep pattern /dev/null

FWIW, I have learned about gnu-grep's -r option reading this
thread, which I had not noticed previously.  I guess that just
goes to show that old habits die hard :-).

INTERNET:   bill at  Bill Campbell; Celestial Software LLC
URL:  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186

Good luck to all you optimists out there who think Microsoft can deliver
35 million lines of quality code on which you can operate your business.
   -- John C. Dvorak

More information about the freebsd-questions mailing list