You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用rsync在单次同步中从源目录复制指定文件到目标目录?

单次rsync同步指定多个文件的简化方法

嘿,我懂你不想用循环挨个同步文件的心情——其实rsync本身就支持单次命令同步多个特定文件,完全不用写循环脚本,一步到位就行!

核心命令写法

最直接的方式就是把要同步的多个文件路径依次列在rsync命令里,比如:

rsync -av /path/to/source/file1.jpg /path/to/source/file2.jpg /path/to/destination/

如果所有目标文件都在同一个源目录下,还可以用大括号扩展来简化路径输入,避免重复写源目录:

rsync -av /path/to/source/{file1.jpg,file2.jpg} /path/to/destination/

关键选项说明

  • -a:归档模式,会自动保留文件的权限、时间戳、软链接等元数据,是日常同步最常用的选项
  • -v:verbose模式,会显示同步的详细过程,方便你确认哪些文件被处理了
  • 如果担心目标目录不存在导致同步失败,可以加上--mkpath(rsync 3.0.0及以上版本支持),它会自动创建目标目录的层级:
    rsync -av --mkpath /path/to/source/{file1.jpg,file2.jpg} /path/to/destination/
    

对比循环方式的优势

之前你可能用类似这样的循环:

for file in file1.jpg file2.jpg; do
    rsync -av /path/to/source/$file /path/to/destination/
done

而单次rsync命令只需要启动一次进程,效率更高,命令也更简洁易读,尤其是当需要同步的文件数量不多时,体验提升非常明显。

特殊情况处理

如果文件名包含空格或特殊字符,记得用引号包裹路径,比如:

rsync -av "/path/to/source/file 1.jpg" "/path/to/source/file 2.jpg" /path/to/destination/

或者在大括号里转义特殊字符:

rsync -av /path/to/source/{file\ 1.jpg,file\ 2.jpg} /path/to/destination/

内容的提问来源于stack exchange,提问作者Mohammed Mohideen M Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:48:37