I'm using Mac 10.9.5 with bash shell. I'm trying to search for instances of text within a group of files, but for some reason, in one particular directory, I get this bizarre "Unterminated quote" error …
Daves-MacBook-Pro:sbadmin davea$ find src/main/webapp/WEB-INF/ -name "*" | xargs grep 'addresses' > /tmp/addr
xargs: unterminated quote
Any ideas how to solve this?
Best Answer
That happens because one of the filenames contains a quote and is passed to
xargs
as-is. That meansxargs
is run likexargs grep 'addresses' > /tmp/addr some file'name
— and there is an unterminated'
here.The actual problem is that you're using
find | xargs
. That's something you really don't want to do, even if it looks tempting.There are a few solutions to this problem:
Use the
-print0
option forfind
and-0
forxargs
:This is the recommended way to pipe from
find
toxargs
because it can deal with any filename, even those containing newlines.Note that using
-name "*"
is superfluous. You should also consider using-type f
to filter only files. And you could, of course, call a program from withinfind
, too:But this is also not recommended. See the second option:
Use a more efficient approach altogether – recursive
grep
ing with the-r
option: