如何在for循环中转义冒号:和逗号等字符?

时间:2017-09-02 16:05:07

标签: bash rsync cp

我有一个名为“blocked.txt”的文件,其中包含4000个文件的名称,如下所示:

1502146676.VdeI4b5c5cbM804631.vps47619.domain.local:2,
1502146676.VdeI4b5c5cdM808282.vps47619.domain.local:2,
1502146677.VdeI4b5c5d3M192892.vps47619.domain.local:2,
1502146677.VdeI4b5c5d7M213070.vps47619.domain.local:2,
1502146677.VdeI4b5c5e5M796312.vps47619.domain.local:2,
1502146678.VdeI4b5c5efM412992.vps47619.domain.local:2,
1502146678.VdeI4b5c5f1M613275.vps47619.domain.local:2,
1502146679.VdeI4b5c5f8M11301.vps47619.domain.local:2,
1502146682.VdeI4b5c66dM115848.vps47619.domain.local:2,S
1502146682.VdeI4b5c676M608733.vps47619.domain.local:2,
1502146685.VdeI4b5c69aM1652.vps47619.domain.local:2,
....
 ....

我在shell上运行以下命令将文件复制到/ tmp / backup目录

for i in `cat blocked.txt`; do cp -f  "${i}" /tmp/backup/ ; done 

但是这给了我错误“你想要覆盖吗?y / n”,即使我使用-f和cp

任何想法在命令中都有错吗?

1 个答案:

答案 0 :(得分:1)

您可能有别名,

alias cp="cp -i"

或功能

cp () {
    command cp -i "$@"
}

干扰。

要解决此问题,只需使用command cp而不只是cp

while read -r name; do
    command cp "$name" /tmp/backup
done <blocked.txt

或指定cp的完整路径:

while read -r name; do
    /bin/cp "$name" /tmp/backup
done <blocked.txt