Grep Guru

Giorgos Keramidas keramida at
Mon Jun 9 14:25:12 UTC 2008

On Sun, 8 Jun 2008 16:07:12 -0700, Bill Campbell <freebsd at> wrote:
> 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.

The \+ trick behaves like xargs, so this shouldn't be an issue :)

> This is of particular importance if one wants to see the file
> names in the output.

You can ensure the same even if xargs picks up a single file to grep

    xargs -0 grep pattern /dev/null

This will cost an open() / read() pair for each batch of files, but it
ensures that grep will always see at least two file names, and it should
print the filename of any matching files.

More information about the freebsd-questions mailing list