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 quoteAny ideas how to solve this?
1 Answer
That happens because one of the filenames contains a quote and is passed to xargs as-is. That means xargs is run like xargs 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
-print0option forfindand-0forxargs:find src/main/webapp/WEB-INF/ -name "*" -print0 | xargs -0 grep 'addresses' > /tmp/addrThis is the recommended way to pipe from
findtoxargsbecause it can deal with any filename, even those containing newlines.Note that using
-name "*"is superfluous. You should also consider using-type fto filter only files. And you could, of course, call a program from withinfind, too:find … -type f -exec grep 'addresses' {} \; > /tmp/addrBut this is also not recommended. See the second option:
Use a more efficient approach altogether – recursive
greping with the-roption:grep -r 'addresses' src/main/webapp/WEB-INF/ > /tmp/addr