51

I want to get the number of lines in my application. I am using this code:

find . "(" -name "*.m" -or -name "*.h" ")" -print | xargs wc -l

It is working fine in other applications but for one of my applications it is giving the error "xargs unterminated quote".

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Lena
  • 1,400
  • 3
  • 14
  • 20

5 Answers5

94

Does one of your filenames have a quote in it? Try something like this:

find . "(" -name "*.m" -or -name "*.h" ")" -print0 | xargs -0 wc -l

The -print0 argument tells find to use the NULL character to terminate each name that it prints out. The -0 argument tells xargs that its input tokens are NULL-terminated. This avoids issues with characters that otherwise would be treated as special, like quotes.

Gary G
  • 5,692
  • 2
  • 27
  • 18
18

This can happen because you have a single quote in a filename somewhere...

i.e., -> '

To find the problem file, run the following in the terminal:

\find . | grep \' 

you can also run xargs like so to effectively address this issue:

xargs -I

and it can also happen if you have an alias for xargs setup that's causing an issue. To test if this is the case, just run xargs with a \ in front of it, e.g.

\find . | \xargs ....

The \ simply means "run the command without any aliases"

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
5

The canonical way to solve quotes, spaces and special characters problems when using find is to use the -exec option instead of xargs.

For your case you can use:

find . "(" -name "*.m" -or -name "*.h" ")" -exec wc -l "{}" \;
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
1

After some tinkering, I found that this command worked for me (because I had spaces and unmatched quotations in my filenames):

find . -iname "*USA*" -exec cp "{}" /Directory/to/put/file/ \;

. refers to the location the search is being run

-iname followed by the expression refers to the match criteria

-exec cp "{}" /Directory/to/put/file/ \; tells the command to execute the copy command where each file found via -iname replaces "{}"

You need the \; to denote to the exec command that the cp statement is ending.

ngopal
  • 73
  • 1
  • 4
0

Solved by replacing from source " with \"

Ax_
  • 803
  • 8
  • 11