scp/rsync 远程传输速查表
把 scp 和 rsync 最常用的远程传输选项整理成速查表,服务器间文件传输时对照查。
返回 系统与运维scp 基础传输 8
scp file user@host:/path上传文件到远程
scp user@host:/path/file .从远程下载文件
scp -r dir user@host:/path递归上传目录
scp -P 2222 file user@host:/path指定端口
scp -i ~/.ssh/id_rsa file user@host:/path指定私钥
scp -C file user@host:/path启用压缩
scp -q file user@host:/path静默模式
scp -v file user@host:/path详细输出
rsync 基础同步 8
rsync file user@host:/path同步文件到远程
rsync user@host:/path/file .从远程同步文件
rsync -r dir user@host:/path递归同步目录
rsync -a dir user@host:/path归档模式(保留权限、时间戳等)
rsync -av dir user@host:/path归档模式并详细输出
rsync -az dir user@host:/path归档模式并压缩传输
rsync -e "ssh -p 2222" file user@host:/path指定 SSH 端口
rsync -e "ssh -i key" file user@host:/path指定 SSH 私钥
rsync 增量同步 6
rsync -av --delete src/ user@host:/path/同步并删除目标端多余文件
rsync -av --update src/ user@host:/path/只同步更新的文件
rsync -av --ignore-existing src/ user@host:/path/跳过已存在的文件
rsync -av --dry-run src/ user@host:/path/模拟运行(不实际传输)
rsync -av --progress src/ user@host:/path/显示传输进度
rsync -av --partial src/ user@host:/path/保留部分传输的文件(断点续传)
rsync 过滤选项 5
rsync -av --include="*.txt" src/ dest/只同步指定类型
rsync -av --exclude="*.tmp" src/ dest/排除指定类型
rsync -av --exclude=".git" --exclude="node_modules" src/ dest/排除多个目录
rsync -av --exclude-from=exclude.txt src/ dest/从文件读取排除规则
rsync -av --filter="+ *.txt" --filter="- *" src/ dest/使用过滤规则
rsync 高级选项 6
rsync -av --compress src/ dest/压缩传输
rsync -av --checksum src/ dest/使用校验和比较文件
rsync -av --size-only src/ dest/只比较文件大小
rsync -av --modify-window=1 src/ dest/修改时间窗口(FAT 文件系统)
rsync -av --log-file=rsync.log src/ dest/记录日志
rsync -av --stats src/ dest/显示统计信息
常用场景 Common Patterns 6
rsync -avz --progress --partial file user@host:/path断点续传大文件
rsync -avz --delete src/ user@host:/path/镜像同步目录
rsync -avz --exclude=".git" --exclude="node_modules" src/ dest/同步代码(排除依赖)
rsync -avz --backup --backup-dir=backup src/ dest/同步并备份修改的文件
rsync -avz --max-size=100m src/ dest/限制传输文件大小
rsync -avz --bwlimit=1000 src/ dest/限制带宽(KB/s)
💡 提示
- scp 适合简单文件传输,rsync 适合目录同步和增量传输。
- rsync 的 --delete 会删除目标端多余文件,使用前确认目标端没有重要数据。
- rsync 的 src/ 和 src 不同:src/ 同步目录内容,src 同步目录本身。