linux反拷文件命令
linux反拷文件命令详细介绍
在 Linux 系统里,“反拷文件” 一般指的是把文件从远程服务器复制到本地机器,或者从本地机器复制到远程服务器。下面为你介绍几种常用的文件复制命令:
1. 使用
scp 命令
scp(Secure Copy)是一个在 Linux 系统下基于 SSH 协议的安全文件复制命令,它能在本地与远程服务器之间安全地复制文件和目录。
从远程服务器复制文件到本地
bash
username@remote_host:/path/to/remote/file /path/to/local/directory
username:远程服务器的用户名。remote_host:远程服务器的 IP 地址或者域名。/path/to/remote/file:远程服务器上文件的路径。/path/to/local/directory:本地机器上保存文件的目录路径。
示例
bash
john@192.168.1.100:/home/john/report.txt /home/user/Documents
此命令会把远程服务器 192.168.1.100 上用户 john 家目录下的 report.txt 文件复制到本地机器的 /home/user/Documents 目录。
从本地复制文件到远程服务器
bash
/path/to/local/file username@remote_host:/path/to/remote/directory
/path/to/local/file:本地机器上文件的路径。username:远程服务器的用户名。remote_host:远程服务器的 IP 地址或者域名。/path/to/remote/directory:远程服务器上保存文件的目录路径。
示例
bash
/home/user/Documents/report.txt john@192.168.1.100:/home/john
此命令会把本地机器上 /home/user/Documents 目录下的 report.txt 文件复制到远程服务器 192.168.1.100 上用户 john 的家目录。
2. 使用
rsync 命令
rsync 是一个功能强大的文件同步工具,它能在本地与远程服务器之间高效地复制和同步文件与目录,并且支持增量复制。
从远程服务器复制文件到本地
bash
username@remote_host:/path/to/remote/file /path/to/local/directory
-a:以归档模式传输文件,保留文件的所有属性。-v:显示详细的传输过程。-z:在传输过程中对文件进行压缩,以减少传输的数据量。
示例
bash
john@192.168.1.100:/home/john/report.txt /home/user/Documents
此命令会把远程服务器 192.168.1.100 上用户 john 家目录下的 report.txt 文件复制到本地机器的 /home/user/Documents 目录。
从本地复制文件到远程服务器
bash
/path/to/local/file username@remote_host:/path/to/remote/directory
示例
bash
/home/user/Documents/report.txt john@192.168.1.100:/home/john
此命令会把本地机器上 /home/user/Documents 目录下的 report.txt 文件复制到远程服务器 192.168.1.100 上用户 john 的家目录。
3. 使用
sftp 命令
sftp(Secure File Transfer Protocol)是一种基于 SSH 协议的安全文件传输协议,可用于在本地与远程服务器之间进行文件传输。
连接到远程服务器
bash
username@remote_host
从远程服务器下载文件到本地
bash
get /path/to/remote/file /path/to/local/directory
从本地上传文件到远程服务器
bash
put /path/to/local/file /path/to/remote/directory
示例
bash
john@192.168.1.100sftp get /home/john/report.txt /home/user/Documents
sftp put /home/user/Documents/report.txt /home/john
91234sftp john@192.168.1.100sftp> /home/john/report.txt /home/user/Documentssftp> put /home/user/Documents/report.txt /home/john
这些命令会先连接到远程服务器,然后分别进行文件的下载和上传操作。