cp 和 scp 是 Linux 中用于复制文件的命令, 用于不同的场景。
1. cp 命令
cp 命令用于在本地系统中复制文件和目录。
| 序号 | 命令 (英文) | 参数 (英文) | 说明 | 示例 |
|---|---|---|---|---|
| 1 | cp | -r (recursive) | 递归复制目录及其内容 (Recursive) | cp -r /source/dir /destination/dir 复制整个目录。 |
| 2 | cp | -i (interactive) | 复制时提示用户确认 (Interactive) | cp -i file1.txt file2.txt 如果 file2.txt 存在,提示确认。 |
| 3 | cp | -u (update) | 只在源文件较新时复制文件 (Update) | cp -u file1.txt /destination/ 只在 file1.txt 新时复制。 |
| 4 | cp | -v (verbose) | 显示详细的复制过程 (Verbose) | cp -v file1.txt /destination/ 复制时显示详细信息。 |
| 5 | cp | -a (archive) | 以归档模式复制文件(保留属性) (Archive) | cp -a /source/dir /destination/ 保留文件属性进行复制。 |
示例:
# 复制单个文件
cp file1.txt /destination/
# 递归复制整个目录
cp -r /source/dir /destination/dir
# 提示确认覆盖文件
cp -i file1.txt /destination/
# 只在源文件更新时复制
cp -u file1.txt /destination/
# 显示复制过程
cp -v file1.txt /destination/
2. scp 命令
scp 命令用于在本地和远程主机之间安全地复制文件。
| 序号 | 命令 (英文) | 参数 (英文) | 说明 | 示例 |
|---|---|---|---|---|
| 1 | scp | -r (recursive) | 递归复制目录及其内容 (Recursive) | scp -r /source/dir user@remote:/destination/ 复制整个目录到远程。 |
| 2 | scp | -P (port) | 指定 SSH 端口号 (Port) | scp -P 2222 file1.txt user@remote:/destination/ 指定端口复制。 |
| 3 | scp | -i (identity file) | 指定私钥文件进行身份验证 (Identity File) | scp -i /path/to/private/key file1.txt user@remote:/destination/ |
| 4 | scp | -v (verbose) | 显示详细的复制过程 (Verbose) | scp -v file1.txt user@remote:/destination/ 复制时显示详细信息。 |
| 5 | scp | -q (quiet) | 安静模式,不显示输出 (Quiet) | scp -q file1.txt user@remote:/destination/ 复制时不显示输出。 |
示例:
# 从本地复制文件到远程主机
scp file1.txt user@remote:/destination/
# 递归复制整个目录到远程主机
scp -r /source/dir user@remote:/destination/
# 指定 SSH 端口号进行复制
scp -P 2222 file1.txt user@remote:/destination/
# 使用私钥文件进行身份验证
scp -i /path/to/private/key file1.txt user@remote:/destination/
# 显示详细复制过程
scp -v file1.txt user@remote:/destination/
cp 和 scp 的区别
| 区别 | cp | scp |
|---|---|---|
| 用途 | 在本地系统中复制文件和目录 | 在本地和远程主机之间安全地复制文件 |
| 协议 | 不使用任何协议 | 使用 SSH 协议进行安全传输 |
| 安全性 | 无法加密,传输不安全 | 加密传输,保证数据安全 |
| 使用场景 | 适用于本地文件管理 | 适用于远程文件传输 |
| 速度 | 复制速度受限于本地磁盘速度 | 速度受限于网络带宽和延迟 |
cp 主要用于本地文件的管理,而 scp 适用于需要跨网络传输文件的情况,并提供更高的安全性。

585

被折叠的 条评论
为什么被折叠?



