如何向cp发送文件名列表(带空格)

时间:2011-05-31 16:15:36

标签: arguments filenames spaces backticks cp

Egrep根据内容为我生成了一个文件列表,但是我在尝试将该列表作为参数传递给cp时做错了。 (我的shell是bash)。我以为我会逃避文件名中的空格并将换行符转换为空格,但是cp似乎忽略了管道输入中的转义空格。

示例文件名:2011-05-15\ 14.43.41.txt

$ cp `egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' '` ~/crashreportstemp/

cp: cannot stat `2011-05-15': No such file or directory

当我执行反引号的内容时,我得到的输出可以直接粘贴到cp中。

我也尝试过使用xargs:

$ egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' ' | xargs cp ~/crashreportstemp

但是这会导致cp将最后传递的文件名视为最终的cp参数,忽略我对cp的显式参数:

cp: target `2011-05-30 16.23.30.txt' is not a directory

我显然忽略了这样做的正确方法,请帮忙!

谢谢 - 杰森

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

egrep -l "OS\ version: 10.6" * | sed -e 's/ /\ /g' | tr '\n' ' ' && echo ~/crashreportstemp | xargs cp

在你的版本中,〜/ crashreportstemp作为cp的第一个参数传递,而不是最后一个。