0

I can copy file via SSH by using SCP like this:

cd /root/dir1/dir2/
scp filename root@192.168.0.19:$PWD/

But if on remote server some directories are absent, in example remote server has only /root/ and havn't dir1 and dir2, then I can't do it and I get an error.

How can I do this - to copy file with creating directories which absent via SSH, and how to make it the easiest way?

The easiest way mean that I can get current path only by $PWD, i.e. script must be light moveable without any changes.

Alex
  • 12,578
  • 15
  • 99
  • 195
  • 1
    If you can use ssh you can do a `mkdir` non-interactively as seen in some answers here: http://stackoverflow.com/questions/1340048/how-do-i-create-a-directory-on-remote-host-if-it-doesnt-exist-without-ssh-ing-i – Joe Apr 30 '14 at 23:22
  • Look at `man scp` and check out the `-r` option. – lurker May 01 '14 at 00:36

2 Answers2

1

This command will do it:

rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v"  root@192.168.0.19:"$PWD/"
M. Adel
  • 401
  • 5
  • 7
1

I can make the same directories on the remote servers and copy file to it via SSH by using SCP like this:

cd /root/dir1/dir2/
ssh -n root@192.168.0.19 "mkdir -p '$PWD'"
scp -p filename root@192.168.0.19:$PWD/
Alex
  • 12,578
  • 15
  • 99
  • 195