Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

Transfer Files with Server: scp and sftp #132

Open Qingquan-Li opened 4 years ago

Qingquan-Li commented 4 years ago

一、scp

References: 鸟哥的 Linux 私房菜- 档案异地直接复制: scp 通常使用 sftp (SSH File Transfer Protocol)是因為可能不知道伺服器上面有什麼檔名的檔案存在,如果已經知道伺服器上的檔案檔名了, 那麼最簡單的檔案傳輸則是透過 scp (Secure Copy Protocol)這個指令。

Transferring Sever (Remote) Files to Local

$ scp username@server_ip:<Server file path> <The path to save this file on the local>
$ scp -r username@server_ip:<Server folder path> <The path to save this folder on the local>

Transferring Local Files to Sever (Remote)

$ scp <Local file path> username@server_ip:<The path to save this file on the server>
$ scp -r <Local folder path> username@server_ip:<The path to save this folder on the server>

注意: scp 传输文件到远程 Ubuntu 服务器的某些目录下,可以会报错 Permission denied,此时只需要先用 scp 把文件传输到无权限限制的目录下,然后在远程的 Ubuntu 上执行 $ sudo mv 源文件 目标目录 命令,移动文件。

查看更多 Linux 文件权限:鳥哥的 Linux 私房菜 - Linux 的檔案權限與目錄配置


二、sftp

References: DigitalOcean | How To Use SFTP to Securely Transfer Files with a Remote Server

For example:

$ cd project_directory # Local
$ sftp username@server_ip
sftp> cd project_directory # Server
sftp> put .env # Transferring Local Files (.env) to the Remote System

sftp> get -r db-backup # Transferring Remote Files (db-backup) to the Local System