3

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?

1 Answer 1

7

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:

  1. Use the -print0 option for find and -0 for xargs:

    find src/main/webapp/WEB-INF/ -name "*" -print0 | xargs -0 grep 'addresses' > /tmp/addr
    

    This is the recommended way to pipe from find to xargs 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 within find, too:

    find … -type f -exec grep 'addresses' {} \; > /tmp/addr
    

    But this is also not recommended. See the second option:

  2. Use a more efficient approach altogether – recursive greping with the -r option:

    grep -r 'addresses' src/main/webapp/WEB-INF/ > /tmp/addr
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.