Make scp always overwrite or create directory

Solution 1:

Use this "dot" syntax:

scp -prq server1:dir1/. server2:dir2/

This copies the contents of that directory, rather than the directory itself. And I believe it's more portable than * globbing.

Solution 2:

Normally to control directory creation you need to use a trailing / to imply a complete path but I think this will then fail to create the directory if it doesn't exist:

scp -prq server1:dir1/* server2:dir2/

This could also miss hidden . files due to the * glob expansion (without some tricky shell specific work)

You can approach it differently with ssh and tar.

ssh server1 "cd dir1 && tar -cf - ." | ssh server2 "( mkdir -p dir2; cd dir2 && tar -xf - )"

But this means traffic goes via your local machine.