首页·终端命令

scp

网络

用法

scp [-P <port>] <src> <user>@<host>:<dest>

通过 ssh 一次性复制文件;端口是大写 `-P`,远端路径要带冒号,而体量大或要反复传的场合 rsync 更合适,因为它会跳过没变的文件并能续传。

常用选项

选项含义
-P 2222Port, capital P. Lowercase -p means "preserve timestamps" here.
-rCopy a directory and everything under it.
-i <key>Use a specific private key.
-CCompress in transit.
-3Copy between two remote hosts by routing through your machine.
-OUse the old SCP protocol; OpenSSH 9 and later default to SFTP.

示例

scp -P 2222 build.tar.gz deploy@example.com:/tmp/

Upload one file. The remote side needs the colon.

scp -r deploy@example.com:/var/log ./logs

Download a whole directory.

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. scp 是做什么的?

通过 ssh 一次性复制文件;端口是大写 `-P`,远端路径要带冒号,而体量大或要反复传的场合 rsync 更合适,因为它会跳过没变的文件并能续传。

Q. 怎么写?

scp [-P <port>] <src> <user>@<host>:<dest> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man scp。这条命令属于网络。

相关命令

man 手册: man scp