我正在尝试复制一个目录下的一堆文件,其中一些文件名中有空格和单引号。 当我尝试使用find
,grep
和xargs
一起运行时,我会得到以下错误:
find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote
有没有更加强大的使用 xargs
的建议?
我是在使用 BSD 版本的 xargs
,系统为 Mac OS X 10.5.3(Leopard)。
我正在尝试复制一个目录下的一堆文件,其中一些文件名中有空格和单引号。 当我尝试使用find
,grep
和xargs
一起运行时,我会得到以下错误:
find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote
有没有更加强大的使用 xargs
的建议?
我是在使用 BSD 版本的 xargs
,系统为 Mac OS X 10.5.3(Leopard)。
你可能需要像这样使用grep命令搜索Foobar目录:
find . -name "file.ext"| grep "FooBar" | xargs -i cp -p "{}" .
-i
已经被弃用,应该使用 -I
。 - Asclepiusmapfile
将stdout转换为行数组:find . | grep "FooBar" | (mapfile -t; cp "${MAPFILE[@]}" ~/foobar)
好处有:
您可以将其他参数附加到文件名。对于cp
,您还可以:
find . -name '*FooBar*' -exec cp -t ~/foobar -- {} +
但是,有些命令没有这样的功能。
缺点:
嗯...谁知道OS X上是否有Bash可用呢?
--delimiter
选项(-d
)。尝试使用\n
作为分隔符,这可以防止xargs
将带空格的行分隔成多个单词/参数。 - MattBianco