scp不支持排除机制,需用rsync替代或结合tar/find过滤:rsync原生支持--exclude、--exclude-from;tar可--exclude打包后scp;find+tar管道可边筛选边传。

scp 本身不支持排除文件或目录的语法,它没有类似 rsync 的 --exclude 机制。如果需要跳过某些文件类型(比如 .log、.tmp 或整个 node_modules),不能靠 scp 单独完成,得借助其他工具或预处理方式。
用 rsync 替代 scp(推荐)
rsync 基于 SSH,安全性和 scp 相当,但功能更灵活,原生支持排除规则:
- 排除特定后缀:
rsync -avz --exclude='*.log' --exclude='*.tmp' /local/dir/ user@host:/remote/ - 排除整个目录:
rsync -avz --exclude='node_modules/' --exclude='.git/' /project/ user@host:/var/www/ - 从文件读取排除列表:
rsync -avz --exclude-from=exclude-list.txt /src/ user@host:/dst/(每行一个模式)
先打包再传输(适合简单场景)
用 tar 过滤后再传,适用于不想装 rsync 或只偶尔操作:
- 跳过日志和临时文件打包:
tar --exclude='*.log' --exclude='*.tmp' -czf archive.tgz /data/ - 传过去再解压:
scp archive.tgz user@host:/tmp/ && ssh user@host 'tar -xzf /tmp/archive.tgz -C /target/'
用 find + tar 管道传输(免存中间文件)
避免本地生成大包,边筛选边传:
find /path -type f ! -name "*.log" ! -name "*.tmp" -print0 | tar -cz --null -T - | ssh user@host 'tar -xzf - -C /dest/'- 注意:路径需绝对,且远程目标目录要存在;
-T -表示从标准输入读文件列表
本质上,scp 是个“复制器”,不是“筛选器”。真正需要排除时,绕开它用 rsync 或组合命令更可靠、更常用。











