117

I am trying to copy a folder to remote Ubuntu server using command line ssh connection, i understand it's doable to transfer a file using scp but i have many files in a folder iam trying to copy to that remote server, how is that done? anyone? Thank you.

aero
  • 1,305
  • 3
  • 10
  • 6

1 Answers1

226

You can use secure copy (scp) with the recursive option (-r):

scp -r /path/to/local/dir user@remotehost:/path/to/remote/dir

Alternatively, I recommend rsync because you can resume transfers if the connection breaks, and it intelligently transfers only the differences between files:

rsync -avz -e 'ssh' /path/to/local/dir user@remotehost:/path/to/remote/dir

Note that in both cases you should be careful of trailing slashes: moving /path/to/local/dir to remotehost:/path/to/remote/dir/ results in /path/to/remote/dir/dir

amc
  • 7,142
  • Won't it ask for remote machine's password? I tried the above command and got Host key verification failed. – CKM Mar 30 '17 at 05:16
  • Depends on the ssh server setup, but generally yes, it will use a key to authenticate and fallback to password if it's allowed. – amc Mar 30 '17 at 13:54
  • This is seriously slow. Any better /much faster alternatives? – WestCoastProjects Apr 13 '20 at 22:31
  • Much much faster approach is here https://unix.stackexchange.com/a/10037/66602 – WestCoastProjects Apr 13 '20 at 22:45
  • Sure. If you compress things first then only rsync the tarball (and later decompress) then it could be faster. This doesn’t change the core answer. – amc Apr 13 '20 at 23:06
  • rsync command given here just kept waiting for me. For me, this worked like charm – Atul Apr 16 '20 at 06:50
  • 1
    I have my ssh private key, how can I use it with scp? – Franva May 27 '21 at 05:39
  • It would be great to have an extended answer because it is not clear what must be running on a second machine, and how to get user@remotehost. – Nairum Mar 31 '23 at 13:02
  • the question and answer assume SSH is already setup for the two machines. how to setup ssh access is a separate question and there are plenty of tutorials etc. to answer that, e.g., https://help.ubuntu.com/community/SSH/OpenSSH/Configuring and https://www.digitalocean.com/community/tutorials/ssh-essentials-working-with-ssh-servers-clients-and-keys – amc Apr 05 '23 at 20:08