492

I'm SSHing into a remote server on the command line, and trying to copy a directory onto my local machine with the scp command. However, the remote server returns this "usage" message:

[Stewart:console/ebooks/discostat] jmm% scp -p ./styles/
usage: scp [-1246BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 [...] [[user@]host2:]file2
[Stewart:console/ebooks/discostat] jmm%

I'd like to be able to transfer files in both directions. From what I read, I thought the above command would work for downloading, and scp -p [localpath] [remotepath] for uploading?

binary lobster
  • 444
  • 1
  • 6
  • 14

4 Answers4

795

You need to scp something somewhere. You have scp ./styles/, so you're saying secure copy ./styles/, but not where to copy it to.

Generally, if you want to download, it will go:

# download: remote -> local
scp user@remote_host:remote_file local_file 

where local_file might actually be a directory to put the file you're copying in. To upload, it's the opposite:

# upload: local -> remote
scp local_file user@remote_host:remote_file

If you want to copy a whole directory, you will need -r. Think of scp as like cp, except you can specify a file with user@remote_host:file as well as just local files.

Edit: As noted in a comment, if the usernames on the local and remote hosts are the same, then the user can be omitted when specifying a remote file.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
lemnisca
  • 10,133
  • 3
  • 19
  • 11
  • 3
    Note that if the user is the same on the remote host and the local host, the username can be omitted: scp hello.c myserver.net:~/projects/ – strager Dec 05 '08 at 12:53
  • Yes, true, I'll add a note about that. I included the user because then the examples I gave will always work. :) – lemnisca Dec 05 '08 at 13:10
  • Im having a hardtime on this.. is this correct. scp C:\filename.txt server1@server2:home – Vincent Dec 11 '13 at 03:29
  • Please add bold formatting on "donload" and "upload". – kursus Mar 19 '14 at 19:32
  • 2
    "naturally" (I say naturally because I just wasted 15 minutes on this) you should not be connected to the remote host while attempting to "download" to local, because if you are executing the code from a remote instance, "local" will be interpreted as the "remote", if you see what I mean. So **don't** run ssh first. – PatrickT Apr 01 '14 at 04:09
  • -P for port / -r for folder coping – SkorpEN Mar 04 '16 at 11:17
  • make sure to pass port as `scp -P port local_file user@remote_host:remote_file` and `scp -P port user@remote_host:remote_file local_file` – iamtodor May 21 '18 at 10:07
  • +1 I keep coming back to this. I love that the answer is extremely clear on defining the direction of transfer and order of parameters – Bradley4 Sep 14 '18 at 18:41
  • If you want to use credentials don't forget to set the -i flag: scp -i rsa_file_path user@remote_host:remote_file local_file This is recommended over passwords. – Lukas N.P. Egger Oct 16 '18 at 20:43
178

If copying to/from your desktop machine, use WinSCP, or if on Linux, Nautilus supports SCP via the Connect To Server option.

scp can only copy files to a machine running sshd, hence you need to run the client software on the remote machine from the one you are running scp on.

If copying on the command line, use:

# copy from local machine to remote machine
scp localfile user@host:/path/to/whereyouwant/thefile

or

# copy from remote machine to local machine
scp user@host:/path/to/remotefile localfile
JeeBee
  • 17,476
  • 5
  • 50
  • 60
  • 4
    You are the man. I deployed a blog in less than 10 seconds without any additional installation on server side! – P.M Oct 04 '13 at 04:28
  • 2
    This worked perfectly. I used msysgit in cmd since it has both scp and ssh. – Ibn Saeed Jun 14 '14 at 16:40
  • 3
    This worked perfectly. I used scp * user@host:/path/to/where – Bastin Robin Jan 08 '15 at 21:08
  • 3
    That should be the top answer. – gsamaras Jun 16 '15 at 16:47
  • 1
    If I have a file on my laptop (mac using terminal) surely I run the command to copy from that to the server I want the file on. How could a remote server access my laptop that isn't set up as some kind of server? When trying this I get - Permission denied (publickey) I think I need to get my login somehow automated as I pass the path every time to my key when logging in. – landed Jun 14 '17 at 09:10
  • 3
    Thanks for the WinSCP suggestion. Way easier than trying to write all those commands manually... – Brian Leishman Aug 04 '17 at 21:23
  • 1
    Good mention of Nautilus having SCP integrated. Way easier for adhoc transfers than messing around with terminal. Basically your server connection is mounted like a volume. – Rich Feb 10 '18 at 10:18
  • 1
    Is there a linux alternative to WinSCP? – TetraDev May 17 '18 at 20:33
  • If you have to pass a key just `scp -i ~/.ssh/id_rsa.pub FILENAME USER@SERVER:/home/USER/FILENAME` – D0rm1nd0 Oct 19 '20 at 13:27
23

You need to specify both source and destination, and if you want to copy directories you should look at the -r option.

So to recursively copy /home/user/whatever from remote server to your current directory:

scp -pr user@remoteserver:whatever .
Ken
  • 77,016
  • 30
  • 84
  • 101
20

No, you still need to scp [from] [to] whichever way you're copying

The difference is, you need to scp -p server:serverpath localpath

Gareth
  • 133,157
  • 36
  • 148
  • 157