How do I create a directory on remote host if it doesn't exist without ssh-ing in?

后端 未结 6 1979
南笙
南笙 2021-02-01 00:57

I\'m not sure if this is possible or not. Basically, I\'m writing a script that allows me to scp a file to my hosting. This is it so far. Argument 1 is the file and argument 2 i

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 01:36

    I assume you mean you don't want to interactively log in and create directories by hand, rather than that you want to avoid using ssh altogether, since you still need a password or public key with scp.

    If using ssh non-interactively is acceptable, then you can stream your file using cat over ssh:

    cat $1 | ssh $2 "mkdir $3;cat >> $3/$1"
    

    where

    $1 = filename 
    $2 = user@server
    $3 = dir_on_server
    

    If the directory already exists, mkdir complains but the file is still copied over. The existing directory will not be overwritten. If the directory does not exist, mkdir will create it.

提交回复
热议问题