运行 rsync -av `cat /path/to/file` /destination/ 时转义文件路径上的空格

Escape spaces on file paths when running rsync -av `cat /path/to/file` /destination/

我有一个纯文本文件,其中包含一个路径列表(大约 120 个,每行一个),我需要将其 rsync 到一个目录中,为此,我使用 this suggestion

rsync -av `cat /path/to/file` /destination/

问题是路径包含用 \ 转义的空格,因此它们看起来像这样:

/path/to\ some/directory-001/
/path/to\ some/directory-003/
...
/path/to\ some/directory-120/

当我运行上面的命令时,我得到:

rsync: link_stat "/path/to" failed: No such file or directory (2)
rsync: link_stat "/path/to/some/directory-001/" failed: No such file or directory (2)

如果路径中没有空格,它会完美运行。我尝试将路径用双引号、单引号括起来,删除带或不带引号的 \。

如果有办法解决这个问题,请告诉我,我可能在实际命令中遗漏了什么?

谢谢!

您可以使用双引号 " 来使用带空格的 file/dir,例如使用 xargs:

$ xargs < file -L1 -I {} rsync -av "{}" /destination/

在这种情况下,带有路径的文件将被传递给 xargs,文件将逐行读取 -L1 并使用 {} 作为该行的占位符。

如果你想 运行 并行,你也可以使用选项 -P,例如,如果你有 4 个核心,你可以使用这样的东西:

$ xargs < file -P4 -L1 -I {} rsync -av "{}" /destination/

要在源上显示完整路径,您可以尝试类似的操作:

$ xargs < file -L1 -I {} sh -c "echo $(pwd -P)/{}; rsync -av {} /destination/"