9

How do I transfer files from a computer I am accessing remotely back to the computer I'm currently at?

Can anyone help me?

Here's the syntax I know so far,

scp (filename) (I don't know what to put here): (location on my computer)

deubeuliou
  • 174
  • 8
Mechy
  • 259
  • 1
  • 4
  • 14

4 Answers4

12

scp is actually easier to use than appears at first glance.

scp <from> <to>

<from> or <to> can be local or remote.

Remote files are of the form user@host:path_on_remote

Local files are just simple paths: /path/to/my/file.txt

If you are doing this by hand, you might find it easier not to copy the file back to your office machine, by running the following from the localhost before being logged into the remote host:

scp user@remotehost:/path/to/my/file/on/remote.txt /local/path/local.txt

If you need to copy a directory - as has already been mentioned - you can give scp the -r option.

When logged into a remote, copying back is basically the same, though you'll need ssh running on your local machine, which may require port-fowarding on your local router. I tend to find it easier calling scp on my laptop to the server rather than the other way around.

8

If your computer is Internet accessible, this - hacky as it is - should work;

scp (filename) `echo $SSH_CLIENT | awk '{print $1}'`:(location on my computer)

The easier way (that will always work) is to do the scp from a new window on your local computer instead;

scp (remote computer):path/to/remote/file (location on my local computer)

remote computer being the same address you normally ssh to.

As an example, if the file is on the remote computer called remotecomputer.com in a subdirectory of your home directory called important and the file is called test.txt, you can issue this command on the local computer to copy it to the current directory on the local computer;

scp remotecomputer.com:important/test.txt .
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • I'm having trouble figuring out the path though. For some reason, it just won't work. – Mechy May 19 '13 at 21:23
  • How do I find out what to put in the bubble, (local computer)? – Mechy May 19 '13 at 21:27
  • Well, the problem is I'm not sure how to do the pathing. I've tried something like /home/usernamefolder/(myfile) but it can't find the darn file for some reason. Where do you start from when you do paths? – Mechy May 19 '13 at 21:29
  • Is there a way I can find the address of my local computer that I'm connecting to the remote computer from? – Mechy May 19 '13 at 21:30
  • @Mechy `echo $SSH_CLIENT` on the remote computer. – Joachim Isaksson May 19 '13 at 21:30
  • That's great! Do I just put my own username before that? e.g. Mechy@(ipaddress) – Mechy May 19 '13 at 21:33
  • @Mechy Then your local computer either does not run an SSH server or there's a firewall or NAT router involved. Either means doing scp from your local machine is the only option. – Joachim Isaksson May 19 '13 at 21:37
  • How do I figure out the path for where my file is? I keep trying, but it tells me that it can't find the file. Is there a command I can type to figure out the path to the file? Thanks for the help so far by the way. – Mechy May 19 '13 at 21:39
  • @Mechy Go to the directory where the file is and type `pwd`, that will tell you the path you need to use. Just add the file name at the end and you'll have the absolute path of the file. – Joachim Isaksson May 19 '13 at 21:41
  • 2
    @Mechy If you're logging into the remote server using public-key authentication (passwordless login) you can do tab-completion in the same way as you would in a normal `bash` session. I find this very helpful as it means I don't have to create an `ssh` session just to copy the path. –  May 19 '13 at 21:44
  • 1
    I figured it out. When I did scp (host):(I put space here)/address That was the problem. – Mechy May 19 '13 at 21:44
  • Without ssh server on your computer and router port forwarded to your ssh server or DMZ ip exposure, this will not work. – juslintek Aug 23 '20 at 11:03
2

with pem file(pem file need to located in root)

$ scp -i <pem.pem> <file_on_local> <user>@<remotehost>:/path/to/file

with credentials

$ scp <file_on_local> <user>@<remotehost>:/path/to/file
tk_
  • 16,415
  • 8
  • 80
  • 90
1

From the man page, man scp...

scp file_on_local user@host.name:/path/to/file

If you add a -r, it can do a whole dir.

demure
  • 7,337
  • 3
  • 22
  • 17