19

Situation: I have 2 computers: Pc_A and Pc_B. I am also using a SSH connection that goes from A to B.

My problem: I have a folder saved on the desktop of Pc_A and I need to copy it to the desktop of Pc_B via terminal. The thing is that once I open the terminal on Pc_A and I connect to Pc_B I'm no longer capable of transferring data from one computer to another...

Question: does anyone have the idea of what should be done in such case?

  • What do you mean exactly? You open a SSH connection from PCA to PCB, and when you do that, you're not able to copy anymore? – Exeleration-G Dec 12 '14 at 23:55
  • 1
    when I open the terminal and i connect through ssh i can only see what i have on PCB; therefore if i wished to copy a folder from A to B i can't because i'm not able to select my files through the same terminal; the only way i can select my files is by opening a new terminal so i don't understand how i can copy a file form the terminal that is seeing PCA to the terminal connected with PCB – Federico Gentile Dec 12 '14 at 23:58
  • I see. Use one of the answers we posted here (also, as I see that you're new here, press the checkmark on one of them if that answer works out for you). – Exeleration-G Dec 12 '14 at 23:59

2 Answers2

34

You could use scp:

When you're on PCB:

scp -r your_user_name@ip_address_of_PCA:/path/to/remote/directory /path/to/local/directory
17

On PC A, instead of connecting to PC B by ssh, just run

rsync /path/to/local/file username@PCB:/path/to/remote/destination

You could also use scp instead of rsync, with similar formatting for the rest of the line, but I prefer rsync, since it's more powerful, and (I think) verifies after copying. See man rsync for more details. N.B. that the remote computer must have rsync installed too (see comments by neon_overload), otherwise scp would be preferred.

If installed on both computers, rsync will take advantage of the processing power of both. For example, it can compress files before transfer, by using the -z flag.

Sparhawk
  • 6,929
  • More info: This works because rsync can transfer over SSH to other computers that have rsync installed: PCB: signifies a remote connection over SSH. – thomasrutter Dec 13 '14 at 01:30
  • @neon_overload I thought that having rsync installed on the remote computer was not necessary, but if it were, then this could potentially speed up transfers. I can't seem to find a reference though. – Sparhawk Dec 13 '14 at 01:33
  • It's pretty standard for rsync to be on any given system, and available with just the command "rsync", so it usually "just works". It connects via SSH, starts rsync on the remote system with the "rsync" command, and then the rsync processes at each end communicate with each other over the already open SSH connection. All this works so transparently to the user that it's deceptively simple, but you benefit from an rsync process at each end to compare file sizes, modification times and do checksums to save data transfer. – thomasrutter Dec 13 '14 at 10:55
  • FWIW this says that "rsync" must be in the logged in user's execution path on the remote computer. http://troy.jdmz.net/rsync/ – thomasrutter Dec 13 '14 at 10:56
  • @neon_overload Yes, I agree with your first reply. However, I'm not sure if it may still work if the remote computer doesn't have rsync installed. Your link says to make sure rsync is installed remotely, but this might just be for optimisation, and not a necessity. – Sparhawk Dec 13 '14 at 11:05
  • 2
    Fair question, so I just tried removing rsync on a remote machine and trying to rsync to it. I get: bash: rsync: command not found rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]. Replacing rsync on the remote machine fixed it. – thomasrutter Dec 13 '14 at 11:14
  • @neon_overload Huh, interesting! Thanks for testing it! Previously, I was of the opinion that rsync was superior to scp in every single way, and there was never a reason to use the latter. This at least gives one situation where scp might be preferred. Thanks again and I'll edit the answer. – Sparhawk Dec 13 '14 at 11:43