46

I am using ssh to connect to a remote machine.

Is there a way i can copy an entire directory from a local machine to the remote machine?

I found this link to do it the other way round i.e copying from remote machine to local machine.

Community
  • 1
  • 1
nish
  • 6,952
  • 18
  • 74
  • 128
  • It should work ! But unless you give us some error message or missbehaviour instead of saying "doesn't work", we have no way to debug. – hivert Jul 24 '13 at 08:03
  • @hivert : You are right, its working. I dunno what i did wrong the first time. I dont remember the error exactly but i am unable to reproduce it. Thanks anyway :) – nish Jul 24 '13 at 08:09
  • The morality is "always copy paste error message when asking on stackoverflow" ;-) – hivert Jul 24 '13 at 08:17
  • @hivert: loaded to memory :) – nish Jul 24 '13 at 09:11

5 Answers5

62

Easiest way is scp

scp -r /path/to/local/storage user@remote.host:/path/to/copy

rsync is best for when you want to update versions where it has been previously copied.

If that doesn't work, rerun with -v and see what the error is.

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
  • What is the lowercase "a" option ?, I can't see any mention in the manual. – Apex Sep 02 '21 at 04:34
  • a is normally the archive flag, but I can't find it as a valid option on any of the boxes I have access to now, so it has either been removed, or was never there. Either way, it was safe to omit. – Oliver Matthews Nov 18 '21 at 06:53
19

It is very easy with rsync as well:

rsync /path/to/local/storage user@remote.host:/path/to/copy

I recommend the usage of rsync over scp, because it is highly likely that you will one day need a feature that rsync offers and then you benefit from your experience with the tool.

Kenly
  • 24,317
  • 7
  • 44
  • 60
mnagel
  • 6,729
  • 4
  • 31
  • 66
10

This is worked for me

rsync -avz -e 'ssh' /path/to/local/dir user@remotehost:/path/to/remote/dir
toesslab
  • 5,092
  • 8
  • 43
  • 62
Mike
  • 129
  • 1
  • 7
2

this is if you have to used another ssh port other than 22

rsync -avzh -e 'ssh -p sshPort' /my/local/dir/ remoteUser@host:/path/to/remote/dir

this works if your remote server uses default 22 port

rsync -avzh /my/local/dir/ remoteUser@host:/path/to/remote/dir 

This worked for me. Follow this link for detailed understanding.

Benson Okello
  • 354
  • 3
  • 12
0

we can do this by using scp command for example:

 scp -r /path/to/local/machine/directory user@remotehost(server IP Address):/path/to/sever/directory

In case of differnt port

By default, the SCP protocol operates on port 22 but this can be overridden by supplying the -P flag, followed by the port number for example:

scp  -P 8563 -r /path/to/local/machine/directory user@remotehost(server IP Address):/path/to/sever/directory

NOTE: we use -r flag to copy directory's files/folders recursively instead of a single file.

riya
  • 1