Categories
command-line copy scp shell ssh

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

3135

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?

4

  • 93

    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, 2015 at 13:04


  • 8

    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 [email protected]:/remote-dir/remote-file /local-dir/local-file

    Jul 6, 2017 at 13:11


  • use mc: TAB, cd sh://[email protected], use the mc shortcuts, cd out when done.

    – sjas

    Oct 15, 2018 at 3: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 at 14:52


5697

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

By not including the trailing “https://stackoverflow.com/” 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

15

  • 1767

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

    – cptloop

    Nov 26, 2013 at 12:25

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

    Jun 26, 2014 at 20:48

  • 102

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

    – Ber

    May 7, 2016 at 2:06

  • 34

    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, 2018 at 19:45

  • 15

    @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

    – Ioannis

    Dec 11, 2018 at 13:08

342

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

9

  • 15

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

    – dmastylo

    Mar 1, 2014 at 20:27

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

    – Bernhard

    Mar 4, 2014 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.

    Mar 6, 2014 at 3:30

  • 2

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

    Mar 6, 2014 at 6:16

  • 1

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

    – Gladiator

    Mar 10, 2014 at 9:32

306

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

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

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

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

Custom Port where xxxx is custom port number

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

Copy on current directory from Remote to Local

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

9

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

    May 22, 2017 at 13:42

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

    May 23, 2017 at 18:53

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

    May 25, 2017 at 3:10

  • What should i put if the directory contain a space?

    Jun 17, 2017 at 22:43

  • @hyprfrcb Use pwd to get location and use same

    Oct 24, 2017 at 6:03