By Josh Maurice


2008-12-05 12:46:03 8 Comments

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]
           [[[email protected]]host1:]file1 [...] [[[email protected]]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?

4 comments

@lemnisca 2008-12-05 12:51:02

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 [email protected]_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 [email protected]_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 [email protected]_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.

@strager 2008-12-05 12:53:04

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/

@lemnisca 2008-12-05 13:10:27

Yes, true, I'll add a note about that. I included the user because then the examples I gave will always work. :)

@Vincent 2013-12-11 03:29:00

Im having a hardtime on this.. is this correct. scp C:\filename.txt [email protected]:home

@kursus 2014-03-19 19:32:41

Please add bold formatting on "donload" and "upload".

@PatrickT 2014-04-01 04:09:44

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

@SkorpEN 2016-03-04 11:17:13

-P for port / -r for folder coping

@iamtodor 2018-05-21 10:07:22

make sure to pass port as scp -P port local_file [email protected]_host:remote_file and scp -P port [email protected]_host:remote_file local_file

@Bradley4 2018-09-14 18:41:26

+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

@Lukas N.P. Egger 2018-10-16 20:43:50

If you want to use credentials don't forget to set the -i flag: scp -i rsa_file_path [email protected]_host:remote_file local_file This is recommended over passwords.

@Ken 2008-12-05 13:25:00

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

@JeeBee 2008-12-05 12:57:03

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 [email protected]:/path/to/whereyouwant/thefile

or

# copy from remote machine to local machine
scp [email protected]:/path/to/remotefile localfile

@P.M 2013-10-04 04:28:42

You are the man. I deployed a blog in less than 10 seconds without any additional installation on server side!

@Ibn Saeed 2014-06-14 16:40:43

This worked perfectly. I used msysgit in cmd since it has both scp and ssh.

@Bastin Robin 2015-01-08 21:08:42

This worked perfectly. I used scp * [email protected]:/path/to/where

@gsamaras 2015-06-16 16:47:41

That should be the top answer.

@landed 2017-06-14 09:10:24

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.

@Brian Leishman 2017-08-04 21:23:18

Thanks for the WinSCP suggestion. Way easier than trying to write all those commands manually...

@Rich 2018-02-10 10:18:57

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.

@TetraDev 2018-05-17 20:33:34

Is there a linux alternative to WinSCP?

@Gareth 2008-12-05 12:49:36

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

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

@Qinjie 2017-06-21 02:39:21

what is the purpose of -p?

@pjmil 2018-08-13 02:17:37

From man scp: Preserves modification times, access times, and modes from the original file.

@Gareth 2018-08-14 15:18:05

To be clear -p is only included here because the OP included the flag in their original question. It's not in any way relevant to the answer

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] ssh "permissions are too open" error

  • 2012-02-14 02:02:31
  • Yannick Schall
  • 1482863 View
  • 2166 Score
  • 27 Answer
  • Tags:   permissions ssh

29 Answered Questions

29 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
  • 1148347 View
  • 1191 Score
  • 29 Answer
  • Tags:   git bash shell ssh

17 Answered Questions

[SOLVED] How to use SSH to run a local shell script on a remote machine?

20 Answered Questions

[SOLVED] Best way to use multiple SSH private keys on one client

  • 2010-03-10 18:40:58
  • Justin
  • 462550 View
  • 894 Score
  • 20 Answer
  • Tags:   ssh ssh-keys openssh

6 Answered Questions

4 Answered Questions

[SOLVED] How to download a file from EC2 instance to Local Computer

Sponsored Content