简体   繁体   中英

How can I copy file from local server to remote with creating directories which absent via SSH?

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 , ie script must be light moveable without any changes.

这个命令可以做到:

rsync -ahHv --rsync-path="mkdir -p $PWD && rsync" filename -e "ssh -v"  root@192.168.0.19:"$PWD/"

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM