如何使用 xargs 复制名称中包含空格和引号的文件? [英] How can I use xargs to copy files that have spaces and quotes in their names?
问题描述
我正在尝试复制目录下的一堆文件,并且许多文件的名称中包含空格和单引号.当我尝试将 find
和 grep
与 xargs
串在一起时,出现以下错误:
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
对于更强大地使用 xargs 有什么建议吗?
Any suggestions for a more robust usage of xargs?
这是在 Mac OS X 10.5.3 (Leopard) 和 BSD xargs
.
This is on Mac OS X 10.5.3 (Leopard) with BSD xargs
.
推荐答案
您可以将所有这些组合成一个 find
命令:
You can combine all of that into a single find
command:
find . -iname "*foobar*" -exec cp -- "{}" ~/foo/bar ;
这将处理包含空格的文件名和目录.您可以使用 -name
获得区分大小写的结果.
This will handle filenames and directories with spaces in them. You can use -name
to get case-sensitive results.
注意:传递给 cp
的 --
标志阻止它处理以 -
开头的文件作为选项.
Note: The --
flag passed to cp
prevents it from processing files starting with -
as options.
这篇关于如何使用 xargs 复制名称中包含空格和引号的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!