【问题标题】:How to send cp a list of filenames (with spaces)如何向 cp 发送文件名列表(带空格)
【发布时间】:2011-05-31 16:15:35
【问题描述】:

Egrep 正在为我生成一个基于文件内容的文件列表,但我在尝试将该列表作为参数传递给 cp 时做错了。 (我的外壳是 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

我显然忽略了正确的方法,请帮助!

谢谢 - 杰森

【问题讨论】:

    标签: arguments filenames spaces backticks cp


    【解决方案1】:

    试试这样的:

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

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

    【讨论】:

    • 谢谢!好点子。它几乎可以工作,但 cp 似乎无法辨别最后一个参数是目的地。 “cp:在 `~/crashreportstemp' 之后缺少目标文件操作数” 如果我在 xargs 管道之前复制命令的输出并将其粘贴到 cp 命令的前面,它可以正常工作。也许我需要使用输入重定向?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2022-04-07
    相关资源
    最近更新 更多