By Slasengger


2012-07-03 05:17:58 8 Comments

How to 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 to achieve this?

11 comments

@Salem 2016-04-29 19:44:11

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

scp -r . [email protected]:/usr/share/nginx/www/example.org/

@Jonathan 2017-08-25 21:48:30

Assuming the user had permissions, could you do an absolute path without using [email protected]

@Alexander Yancharuk 2013-12-04 06:21:57

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

@dmastylo 2014-03-01 20:27:15

Did not know about the config file, this is awesome!

@Bernhard 2014-03-04 15:12:48

Tab completion is nonsense, just completes from the local host for me.

@Alexander Yancharuk 2014-03-06 03:30:25

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

@Alexander Yancharuk 2014-03-06 03:34:03

@Bernhard Remote tab completion is a well know reature. It's nonsense just for you :)

@Bernhard 2014-03-06 05:24:46

@AlexanderYancharuk Maybe, but your answer does not do that for me, so your answer is at least restricted to some versions at the local and/or host machine.

@Alexander Yancharuk 2014-03-06 06:16:28

@Bernhard For me is was obvious because I'm using bash-shell. Thanks for pointing me on that! Answer updated.

@blong 2014-03-06 19:16:51

My mistake @AlexanderYancharuk , I thought you were implying that the OP (@Slasengger) would need to modify his ~/.ssh/config in order to copy folders (which is incorrect). I see now that you were trying give him additional convenience with passwordless ssh.

@Gladiator 2014-03-10 09:32:50

@Alexander Yancharuk : Thanks for the answer, this is more detailed than just covering the syntax alone.

@tutuDajuju 2014-09-19 12:16:51

You can also use a tool like storm to automate the process of adding servers to .ssh/config

@dimuthu 2015-06-09 04:28:46

Wow.. great point on remote path completion. I didn't know that.

@Gryphius 2012-07-03 05:21:39

scp -r [email protected]:/path/to/foo /home/user/Desktop/

From man scp (See online manual)

-r Recursively copy entire directories

@cptloop 2013-11-26 12:25:58

I google this every time. Related comic: xkcd.com/1168

@john k 2014-04-27 18:23:29

@Gryphius I continue to get a permission denied (publickey) error when using this. How do I resolve that?

@Gryphius 2014-04-28 03:30:50

@john-k, many possible reasons (scp not enabled on server, public key not authorized, file permission problem, ...). check if standard ssh works with your key first. consult the existing questions on that topic on sof/serverfault and if that doesn't help, ask a new one on serverfault with debug output from ssh / scp.

@Wilf 2014-06-07 17:03:41

You have to set up the RSA key on both machines - you may have to run ssh-add on the local machine as well.

@Automatico 2014-06-26 20:48:00

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 [email protected]:something . as seen in dimuthu's answer

@Allen 2015-01-09 09:35:50

scp -r [email protected]:/path/to/foo/. /home/user/Desktop/foo always copy and replace

@Ber 2016-05-07 02:06:25

use -p to preserve file modification times, permissions, etc! scp -pr [email protected]

@Toskan 2018-01-24 19:45:02

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 2018-01-24 19:52:48

for example this (typically used in shell scripts): su myuser -c "scp -r /var/www/myapp.org/uploads [email protected]$jcrdevip:/var/www/mydevapp.com/uploads" will copy the contents from uploads to uploads. scp -r /var/www/myapp.org/uploads [email protected]$jcrdevip:/var/www/mydevapp.com/uploads seems to copy the folder uploads into the mydevapp.com/uploads folder creating an unwanted subfolder

@Ioannis 2018-12-11 13:08:48

@Toskan with scp -r [email protected]:/path/to/foo /home/user/Desktop/ you should end up with Desktop/foo. With scp -r [email protected]:/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

@FabricioG 2019-09-27 23:55:56

Important to note that you can't be logged into the remote server in the terminal window. In example if I'm working on my desktop I have to be on local terminal window or it won't work.

@Kumar Abhinav 2019-12-11 04:24:54

A very useful answer

@Shiv Singh 2016-05-23 09:38:25

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

scp -r /path/from/destination [email protected]:/path/to/destination

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

scp -r [email protected]:/path/from/destination /path/to/destination

Custom Port where xxxx is custom port number

 scp -r -P xxxx [email protected]:/path/from/destination /path/to/destination

Copy on current directory from Remote to Local

scp -r [email protected]:/path/from/file .

Help:

  1. -r Recursively copy all directories and files
  2. Always use full location from /, Get full location by pwd
  3. scp will replace all existing files
  4. hostname will be hostname or IP address
  5. if custom port is needed (besides port 22) use -P portnumber
  6. . (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

@Adam Plocher 2017-05-22 13:42:03

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

@Adam Plocher 2017-05-23 18:53:55

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

@Adam Plocher 2017-05-25 03:10:49

With ssh, yes. Not with scp (I assume).

@Brethlosze 2017-06-17 22:43:21

What should i put if the directory contain a space?

@Shiv Singh 2017-10-24 06:03:13

@hyprfrcb Use pwd to get location and use same

@Timo 2018-03-12 14:26:58

The ˋPortˋˋmight refer to the local? What if both ports are different?

@Shiv Singh 2018-03-12 15:25:34

@Timo There is no matter because you will do scp after SSH login.

@Timo 2018-03-13 10:28:00

@ShivSingh But if I do remote to local, I do not login.

@Arun G 2015-07-06 07:21:08

Typical scenario,

scp -r -P port [email protected]:/path-to-folder  .

explained with an sample,

scp -r -P 27000 [email protected]:/tmp/hotel_dump .

where,

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

@redoff 2019-07-04 12:17:26

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

@kaiser 2016-09-26 21:54:58

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 [email protected]:/path/to/folder /your/local/target/dir

@Fulvio 2016-08-15 05:47:35

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

$ scp -r [email protected]:/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 [email protected]_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 ?.

@justi 2014-05-29 11:12:01

Better to first compress catalog on remote server:

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

Secondly, download from remote:

scp [email protected]:/path/to/backup.tar.gz .

At the end, extract the files:

tar -xzvf backup.tar.gz

@Automatico 2014-06-26 20:43:53

"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 [email protected]:bigfile ..

@GoodSp33d 2015-08-13 08:51:59

This reminds me of the comment made by @cptloop ! :D xkcd.com/1168

@Ronald 2015-11-09 21:05:10

Go to Files on your unity toolbar

enter image description here

Press Ctrl + l and write [email protected]

The 192.168.1.103 is the host that you want to connect.

The here one example

enter image description here

@dimuthu 2014-06-17 06:02:32

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 [email protected]:/path/to/foo /home/user/Desktop/

@Automatico 2014-06-26 20:42:28

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

@Manish Shrivastava 2014-06-10 07:41:13

What I always use is:

scp -r [email protected]:/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.

@ericmjl 2015-01-12 21:52:37

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

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] How to clone or copy a list?

16 Answered Questions

[SOLVED] scp or sftp copy multiple files with single command

  • 2013-06-02 18:56:08
  • user987654
  • 326086 View
  • 281 Score
  • 16 Answer
  • Tags:   sftp scp

16 Answered Questions

[SOLVED] How do I copy a file in Python?

6 Answered Questions

[SOLVED] How to escape spaces in path during scp copy in Linux?

24 Answered Questions

[SOLVED] How to specify the private SSH-key to use when executing shell command on Git?

  • 2010-12-30 19:42:01
  • Christoffer
  • 959330 View
  • 972 Score
  • 24 Answer
  • Tags:   git bash shell ssh

13 Answered Questions

[SOLVED] How to scp in Python?

  • 2008-10-30 14:09:21
  • Michael Gundlach
  • 234607 View
  • 147 Score
  • 13 Answer
  • Tags:   python ssh paramiko scp

7 Answered Questions

[SOLVED] How does `scp` differ from `rsync`?

  • 2013-11-27 14:05:59
  • Leahcim
  • 134725 View
  • 214 Score
  • 7 Answer
  • Tags:   rsync scp

9 Answered Questions

[SOLVED] scp with port number specified

  • 2012-04-26 20:35:05
  • One Two Three
  • 762499 View
  • 722 Score
  • 9 Answer
  • Tags:   port scp

14 Answered Questions

[SOLVED] How to reload .bash_profile from the command line?

4 Answered Questions

[SOLVED] Transferring files over SSH

  • 2008-12-05 12:46:03
  • Josh Maurice
  • 675333 View
  • 455 Score
  • 4 Answer
  • Tags:   ssh scp

Sponsored Content