Drew Stephens Drew Stephens - 1 month ago 9
Linux Question

How can I use xargs to copy files that have spaces and quotes in their names?

I'm trying to copy a bunch of files below a directory and a number of the files have spaces and single-quotes in their names. When I try to string together

find
and
grep
with
xargs
, I get the following error:

find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote


Any suggestions for a more robust usage of xargs?

This is on MacOS 10.5.3 with BSD
xargs
.

Answer

You might also be able to combine all of that into a single find command:

find . -iname "*foobar*" -exec cp "{}" ~/foo/bar \;

This will handle filenames and directories with spaces in them. You can use -name to get case-sensitive results.

(These command line arguments will work with GNU find; I don't know if they're available with BSD's or OS X's find.)

Comments