3343

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?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Slasengger
  • 33,707
  • 3
  • 14
  • 10
  • 104
    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. – JeffDror Jul 20 '15 at 13:04
  • 13
    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 – jeffmcneill Jul 06 '17 at 13:11
  • use `mc`: `TAB`, `cd sh://USER@HOST`, use the mc shortcuts, `cd` out when done. – sjas Oct 15 '18 at 03:04
  • @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** – ccpizza Jan 21 '22 at 14:52
  • 1
    @jeffmcneill yes your right. But you didn't address directly JeffDror, so I guess most people did not realize that your are answering JeffDror's question. – Adam Sep 11 '22 at 07:28

14 Answers14

6014
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

NateS
  • 5,751
  • 4
  • 49
  • 59
Gryphius
  • 75,626
  • 6
  • 48
  • 54
  • 1894
    I google this every time. Related comic: https://xkcd.com/1168/ – cptloop Nov 26 '13 at 12:25
  • @Gryphius I continue to get a permission denied (publickey) error when using this. How do I resolve that? – john k Apr 27 '14 at 18:23
  • 4
    @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. – Gryphius Apr 28 '14 at 03:30
  • You have to set up the RSA key on both machines - you may have to run `ssh-add` on the local machine as well. – Wilf Jun 07 '14 at 17:03
  • 18
    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](http://stackoverflow.com/a/24256706/741850) – Automatico Jun 26 '14 at 20:48
  • 1
    `scp -r user@your.server.example.com:/path/to/foo/. /home/user/Desktop/foo` always copy and __replace__ – Allen Jan 09 '15 at 09:35
  • 108
    use -p to preserve file modification times, permissions, etc! scp -pr user@... – Ber May 07 '16 at 02:06
  • 35
    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 Jan 24 '18 at 19:45
  • 2
    for example this (typically used in shell scripts): `su myuser -c "scp -r /var/www/myapp.org/uploads root@$jcrdevip:/var/www/mydevapp.com/uploads"` will copy the contents from uploads to uploads. `scp -r /var/www/myapp.org/uploads root@$jcrdevip:/var/www/mydevapp.com/uploads` seems to copy the folder uploads into the `mydevapp.com/uploads` folder creating an unwanted subfolder – Toskan Jan 24 '18 at 19:52
  • 19
    @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` – Ioannis Dec 11 '18 at 13:08
  • 1
    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. – FabricioG Sep 27 '19 at 23:55
  • If anyone know the remote server file and he stand in local also using pem file: scp -i pemfile.pem user@remote-server:~/remote/file/location /local/folder/location – Mohammad Habibur Rahman Jan 28 '21 at 06:12
  • Does this command merge remote data with local or it will be an entire replace? – Michael Pacheco Mar 12 '21 at 20:27
  • 2
    @cptloop The link you posted made me read a trillion comic strips in a day, I didn't get any work done and I completely forgot how I got to that site in the first place. Anyhow, thank you for the link....love the comic strips! – Nikster2014 Jul 16 '21 at 07:22
  • you'll never need to google it if you use aliases defined in `~/.ssh/config` — then you can do `scp -r mydir myhost:` or `scp -r myhost:thatdir .` – ccpizza Jan 21 '22 at 14:55
  • 1
    to copy just content without folder I had to place * scp -r user@your.server.example.com:/path/to/foo/* /home/user/Desktop/ – Sultan Zhumatayev Sep 29 '22 at 08:36
359

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • Tab completion is nonsense, just completes from the local host for me. – Bernhard Mar 04 '14 at 15:12
  • 16
    @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 Mar 06 '14 at 03:30
  • @Bernhard Remote tab completion is a well know reature. It's nonsense just for you :) – Alexander Yancharuk Mar 06 '14 at 03:34
  • @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. – Bernhard Mar 06 '14 at 05:24
  • 2
    @Bernhard For me is was obvious because I'm using bash-shell. Thanks for pointing me on that! Answer updated. – Alexander Yancharuk Mar 06 '14 at 06:16
  • 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. – blong Mar 06 '14 at 19:16
  • 1
    @Alexander Yancharuk : Thanks for the answer, this is more detailed than just covering the syntax alone. – Gladiator Mar 10 '14 at 09:32
  • You can also use a tool like [storm](https://github.com/emre/storm) to *automate* the process of adding servers to ```.ssh/config``` – tutuDajuju Sep 19 '14 at 12:16
346

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:

  1. -r Recursively copy all directories and files
  2. Always use full location from /, Get full location/path 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

Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
  • 1
    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 May 22 '17 at 13:42
  • 1
    -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 May 23 '17 at 18:53
  • With ssh, yes. Not with scp (I assume). – Adam Plocher May 25 '17 at 03:10
  • What should i put if the directory contain a space? – Brethlosze Jun 17 '17 at 22:43
  • @hyprfrcb Use `pwd` to get location and use same – Shiv Singh Oct 24 '17 at 06:03
  • The ˋPortˋˋmight refer to the local? What if both ports are different? – Timo Mar 12 '18 at 14:26
  • @Timo There is no matter because you will do scp after SSH login. – Shiv Singh Mar 12 '18 at 15:25
  • @ShivSingh But if I do remote to local, I do not login. – Timo Mar 13 '18 at 10:28
  • As elaborate as this answer is, it's still not clear whether the files of destination will be copied to the other folder destination, or whole folder destination. If you do scp -r .../source .../target then the result will be .../target/source, whereas if you do scp -r .../source/* .../target then .../target will contain the files of source – Robin Manoli May 07 '20 at 06:54
77

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
45

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
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
justi
  • 3,887
  • 2
  • 18
  • 24
24

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
Arun G
  • 1,678
  • 15
  • 17
22

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/
dimuthu
  • 865
  • 11
  • 15
  • 2
    According to [this blog post](https://bbs.archlinux.org/viewtopic.php?id=9107) you get even better performance with `arcfour` in stead of `blowfish`, but it has security flaws. – Automatico Jun 26 '14 at 20:42
21

Go to Files on your unity toolbar

enter image description here

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

enter image description here

Ronald Coarite
  • 4,460
  • 27
  • 31
  • Completely off-topic. Downvoted because it contributes to diluting the relevant answers with a suboptimal and excessively specific approach which doesn't even answer the question. – Valentin Waeselynck Jan 02 '23 at 10:58
16

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
kaiser
  • 21,817
  • 17
  • 90
  • 110
15

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

Fulvio
  • 925
  • 2
  • 13
  • 21
5

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/
Salem
  • 654
  • 7
  • 24
1

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

jeffmcneill
  • 2,052
  • 1
  • 30
  • 26
0

For Windows OS, we used this command.

pscp -r -P 22 hostname@IP:/path/to/Downloads   ./
Manish Gupta
  • 672
  • 6
  • 10
0

It is better to use rsync than scp

Simple one liner :

rsync -aP myfiles/ root@10.14.33.129:server_dir/

a to copy recursively, preserves symbolic links, special and device files, modification times, groups, owners, and permissions. It’s more commonly used than -r and is the recommended flag to use.

P to show progress

General form :

rsync -a ~/dir1 username@remote_host:destination_directory

With root as user, and to a specific IP address.

rsync -aP myfiles/ root@10.14.33.129:server_dir/

You can refer to digitalocean guide for Rsync

Paul Bradbury
  • 482
  • 6
  • 8