How do I copy a folder from remote to local using

2020-01-29 23:06发布

How do I copy a folder from remote to local host using scp?

I use ssh to log in my server.
Then, I would like to copy the remote folder foo to local /home/user/Desktop.

How do I achieve this?

11条回答
ら.Afraid
2楼-- · 2020-01-29 23:57

Better to first compress catalog on remote server:

tar czfP backup.tar.gz /path/to/catalog

Secondly, download from remote:

scp user@your.server.example.com:/path/to/backup.tar.gz .

At the end, extract the files:

tar -xzvf backup.tar.gz
查看更多
聊天终结者
3楼-- · 2020-01-30 00:04

To use full power of scp you need to go through next steps:

  1. Public key authorisation
  2. Create ssh aliases

Then, for example if you have this ~/.ssh/config:

Host test
    User testuser
    HostName test-site.com
    Port 22022

Host prod
    User produser
    HostName production-site.com
    Port 22022

you'll save yourself from password entry and simplify scp syntax like this:

scp -r prod:/path/foo /home/user/Desktop   # copy to local
scp -r prod:/path/foo test:/tmp            # copy from remote prod to remote test

More over, you will be able to use remote path-completion:

scp test:/var/log/  # press tab twice
Display all 151 possibilities? (y or n)

Update:

For enabling remote bash-completion you need to have bash-shell on both <source> and <target> hosts, and properly working bash-completion. For more information see related questions:

How to enable autocompletion for remote paths when using scp?
SCP filename tab completion

查看更多
【Aperson】
4楼-- · 2020-01-30 00:05

I don't know why but I was had to use local folder before source server directive . to make it work

scp -r . root@888.888.888.888:/usr/share/nginx/www/example.org/
查看更多
一夜七次
5楼-- · 2020-01-30 00:06

The question was how to copy a folder from remote to local with scp command.

$ scp -r userRemote@remoteIp:/path/remoteDir /path/localDir

But here is the better way for do it with sftp - SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream.(wikipedia).

$ sftp user_remote@remote_ip

sftp> cd /path/to/remoteDir

sftp> get -r remoteDir

Fetching /path/to/remoteDir to localDir 100% 398 0.4KB/s 00:00

For help about sftp command just type help or ?.

查看更多
放荡不羁爱自由
6楼-- · 2020-01-30 00:09

What I always use is:

scp -r username@IP:/path/to/server/source/folder/  .

. (dot) : it means current folder. so copy from server and paste here only.

IP : can be an IP address like 125.55.41.311 or it can be host like ns1.mysite.com.

查看更多
登录 后发表回答