ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

How do I achieve this?

The OP's question was whether it is possible to copy file from remote to local host while ssh'd to remote host. I'm not sure why no single answer has correctly addressed his/her question.
The premise of the question is incorrect. The idea is, once logged into ssh, how to move files from the logged-in machine back to the client that is logged in. However, scp is not aware of nor can it use the ssh connection. It is making its own connections. So the simple solution is create a new terminal window on the local workstation, and run scp that transfers files from the remote server to local machine. E.g., scp -i key user@remote:/remote-dir/remote-file /local-dir/local-file
use mc: TAB, cd sh://USER@HOST, use the mc shortcuts, cd out when done.
@sjas: in mc it's easier to use Left/Right on the menu > Shell link where you can type the alias you have in your ~/.ssh/config e.g. myhost: > OK

N
NateS
scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/

By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.

From man scp (See online manual)

-r Recursively copy entire directories


I google this every time. Related comic: xkcd.com/1168
Two nice-to-knows I found: the -C flag adds compression and the -c flag lets you pass in other cipher types for better performance, like scp -c blowfish a@b:something . as seen in dimuthu's answer
use -p to preserve file modification times, permissions, etc! scp -pr user@...
This answer lacks important explanation. Will you end up with Desktop/foo or will you have Desktop/allcontentsofFooGohere scp seems to act weird sometimes to me it does one thing then another
@Toskan with scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ you should end up with Desktop/foo. With scp -r user@your.server.example.com:/path/to/foo/. /home/user/Desktop/ you will end up with the contents of foo in Desktop and all the sub-dirs of foo strewn under Desktop
S
Stephen Ostermiller

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

Public key authorisation Create SSH aliases

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

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

Host prod
    User produser
    HostName production-site.example
    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)

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


Did not know about the config file, this is awesome!
Tab completion is nonsense, just completes from the local host for me.
@b.long The question is "How to copy remote folder foo to local Desktop". My answer is "scp -r prod:/path/foo /home/user/Desktop". Hope you're able to see relations.
@Bernhard For me is was obvious because I'm using bash-shell. Thanks for pointing me on that! Answer updated.
@Alexander Yancharuk : Thanks for the answer, this is more detailed than just covering the syntax alone.
S
Shiv Singh

To copy all from Local Location to Remote Location (Upload)

scp -r /path/from/local username@hostname:/path/to/remote

To copy all from Remote Location to Local Location (Download)

scp -r username@hostname:/path/from/remote /path/to/local

Custom Port where xxxx is custom port number

 scp -r -P xxxx username@hostname:/path/from/remote /path/to/local

Copy on current directory from Remote to Local

scp -r username@hostname:/path/from/remote .

Help:

-r Recursively copy all directories and files Always use full location from /, Get full location/path by pwd scp will replace all existing files hostname will be hostname or IP address if custom port is needed (besides port 22) use -P PortNumber . (dot) - it means current working directory, So download/copy from server and paste here only.

Note: Sometimes the custom port will not work due to the port not being allowed in the firewall, so make sure that custom port is allowed in the firewall for incoming and outgoing connection


It seems (at least in recent versions of Raspbian Jessie and Ubuntu) that scp uses -P (uppercase P) for port, while (oddly) ssh uses -p (lowercase).
-p is reserved for preserving "modification times, access times, and modes from the original file". So if you're using that for port, it's probably not working ;-) Unless you have a different version that used the lowercase p differently.
With ssh, yes. Not with scp (I assume).
What should i put if the directory contain a space?
@hyprfrcb Use pwd to get location and use same
S
Stephen Ostermiller

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.example.


Thank you for pointing out that . refers to current directory!
S
Scott Stensland

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

"Better" is highly depends on the data you are transferring and the effort it is to ssh to the server to do zipping/unzipping. And: you can add compression to scp with the -C flag, like scp -C a@b:bigfile ..
This reminds me of the comment made by @cptloop ! :D xkcd.com/1168
You saved me one extra googling.
A
Arun G

Typical scenario,

scp -r -P port username@ip:/path-to-folder  .

explained with an sample,

scp -r -P 27000 abc@10.70.12.12:/tmp/hotel_dump .

where,

port = 27000
username = "abc" , remote server username
path-to-folder = tmp/hotel_dump
. = current local directory

Thank your for the answer. -P for a specific SSH port was helpful for me.
d
dimuthu

And if you have one hell of a files to download from the remote location and if you don't much care about security, try changing the scp default encryption (Triple-DES) to something like 'blowfish'.

This will reduce file copying time drastically.

scp -c blowfish -r user@your.server.example.com:/path/to/foo /home/user/Desktop/

According to this blog post you get even better performance with arcfour in stead of blowfish, but it has security flaws.
R
Ronald Coarite

Go to Files on your unity toolbar

https://i.stack.imgur.com/ZD6gk.png

Press Ctrl + l and write here_goes_your_user_name@192.168.10.123

The 192.168.1.103 is the host that you want to connect.

The here one example

https://i.stack.imgur.com/TFZ4B.png


k
kaiser

In case you run into "Too many authentication failures", specify the exact SSH key you have added to your severs ssh server:

scp -r -i /path/to/local/key user@remote.tld:/path/to/folder /your/local/target/dir

F
Fulvio

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 ?.


S
Salem

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/

Assuming the user had permissions, could you do an absolute path without using root@
M
Manish Gupta

For Windows OS, we used this command.

pscp -r -P 22 hostname@IP:/path/to/Downloads   ./