Linux – Make scp always overwrite or create directory

linuxscp

I am using scp to copy a directory from one remote server to a new directory (IE just changing the name) on another remote server like:

scp -prq server1:dir1 server2:dir2

This works fine if dir2 does not exist on server2, it creates a new directory called dir2 which contains everything from dir1 on server1.

The problem comes when dir2 already exists on server2 (NOTE: I have no way of knowing this in advance or of doing a remove on dir2 on server2 beforehand) – what happens is I get a copy of dir1, called dir1, in dir2.

I am sure there is something basic I am missing, but I just cannot seem to work it out.

Any help much appreciated!

Regards,

Giles

Okay, I have less than 10 rep and cannot be ar$ed to wait 8 hrs so here is what I got:

Here is a script that works for me:

#!/bin/sh
echo "method 1"
scp -prq server1:dir1/* server2:dir2/ >/dev/null  2>&1

if [ "$?" -ne "0" ]; then
        echo "failed ... trying method 2"
        scp -prq server1:dir1 server2:dir2
fi

exit

Still not sure how to do this in a single command or even if possible.

Cheers @mindthemonkey, sometimes just getting a fresh viewpoint can help point the way.

Best Answer

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.