Transferring files over SSH [closed]
Asked Answered
P

4

499

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?

Pentatomic answered 5/12, 2008 at 12:46 Comment(0)
P
804

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.

Permanent answered 5/12, 2008 at 12:51 Comment(9)
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/Sokol
Yes, true, I'll add a note about that. I included the user because then the examples I gave will always work. :)Permanent
Im having a hardtime on this.. is this correct. scp C:\filename.txt server1@server2:homeImmoralist
Please add bold formatting on "donload" and "upload".Cardigan
"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.Heavy
-P for port / -r for folder copingNey
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_fileEtheridge
+1 I keep coming back to this. I love that the answer is extremely clear on defining the direction of transfer and order of parametersAulos
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.Ursa
E
179

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
Elias answered 5/12, 2008 at 12:57 Comment(9)
You are the man. I deployed a blog in less than 10 seconds without any additional installation on server side!Underbodice
This worked perfectly. I used msysgit in cmd since it has both scp and ssh.Siliceous
This worked perfectly. I used scp * user@host:/path/to/whereCrosspurpose
That should be the top answer.Comedy
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.Abb
Thanks for the WinSCP suggestion. Way easier than trying to write all those commands manually...Erythrism
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.Pushcart
Is there a linux alternative to WinSCP?Cummings
If you have to pass a key just scp -i ~/.ssh/id_rsa.pub FILENAME USER@SERVER:/home/USER/FILENAMERectrix
L
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 .
Larianna answered 5/12, 2008 at 13:25 Comment(0)
A
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

Averse answered 5/12, 2008 at 12:49 Comment(3)
what is the purpose of -p?Sturgis
From man scp: Preserves modification times, access times, and modes from the original file.Gluttony
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 answerAverse

© 2022 - 2024 — McMap. All rights reserved.