Friday, January 27, 2012

Using -exec instead of xargs

Using xargs to pipe results of find into grep has problems if find returns files/folders with spaces in the name.

find ./ -name "*html" | xargs grep "something"


Ends up giving me lots of "file doesn't exist" errors.

Using exec circumvents this problem, although using exec is apparently a less efficient way of doing things.

find ./ -name "*html" -exec grep -H "something" "{}" \;

-H tells grep to always show the filename, otherwise you only get the match.
"{}" represents the filename returned by find.
\; I'm not sure what this is, but it has to be there...

Update:
I describe a slightly simpler solution in this posting.

1 comment:

  1. Note, there is more to exec, and other ways to use it, this is one specific solution that may or may not work for other combinations of programs.

    ReplyDelete