bash 如果没有 ssh-ing 目录不存在,如何在远程主机上创建目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1340048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:27:55  来源:igfitidea点击:

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

bashscriptingscp

提问by larjudge

I'm not sure if this is possible or not. Basically, I'm writing a script that allows me to scpa file to my hosting. This is it so far. Argument 1 is the file and argument 2 is the folder I want it to be placed in on the remote server:

我不确定这是否可能。基本上,我正在编写一个脚本,允许我将文件scp到我的主机。到此为止。参数 1 是文件,参数 2 是我希望将其放置在远程服务器上的文件夹:

function upload {
    scp  [email protected]:
}

As you may/may not know, if the directory I specify when I call the function doesn't exist, then the transfer fails. Is there a way to check if the directory exists in the function and if it doesn't, create it.

您可能/可能不知道,如果我在调用函数时指定的目录不存在,则传输失败。有没有办法检查函数中是否存在该目录,如果不存在,则创建它。

I would prefer not having to ssh in every time and create the directory, but if I have got no choice, then I have got no choice.

我宁愿不必每次都 ssh 并创建目录,但如果我别无选择,那么我别无选择。

采纳答案by Peter Mortensen

You can use rsync.

您可以使用rsync.

For example,

例如,

rsync -ave ssh fileToCopy ssh.myhost.net:/some/nonExisting/dirToCopyTO

Note about rsync:

注意事项rsync

rsyncis utility software and network protocol for Unix which synchronizes files and directories from one location to another. It minimizes data transfer sizes by using delta encoding when appropriate using the rsync algorithm which is faster than other tools.

rsync是用于 Unix 的实用软件和网络协议,它将文件和目录从一个位置同步到另一个位置。它通过在适当的时候使用比其他工具更快的 rsync 算法使用增量编码来最小化数据传输大小。

回答by ire_and_curses

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 sshaltogether, since you still need a password or public key with scp.

我假设您的意思是您不想以交互方式登录并手动创建目录,而不是您想ssh完全避免使用,因为您仍然需要密码或带有scp.

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

如果以非交互方式使用 ssh 是可以接受的,那么您可以使用catover流式传输文件ssh

cat  | ssh  "mkdir ;cat >> /"

where

在哪里

 = filename 
 = user@server
 = dir_on_server

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

如果目录已经存在,会mkdir报错,但文件仍会被复制。现有目录不会被覆盖。如果目录不存在,mkdir将创建它。

回答by Matthias Wandel

If you do a recursive scp (-r), it will copy directories as well. So if you create a directory of the name you desire on the remote host locally, copy the file into it, and then recursively copy, the directory will be created, with the file in it.

如果您执行递归 scp (-r),它也会复制目录。因此,如果您在本地远程主机上创建一个您想要的名称的目录,将文件复制到其中,然后递归复制,将创建该目录,其中包含该文件。

Kind of awkward, but it would do the job.

有点尴尬,但它会完成这项工作。

回答by Sarang

This is a two step process

这是一个两步过程

ssh [email protected] "mkdir -p "

This ensures directory structure is created. Then, you copy

这确保创建目录结构。然后你复制

scp  [email protected]:

回答by Rico Chen

How about, for example,
ssh [email protected] '[ -d /tmp/nonexist/dir ] || mkdir -p /tmp/nonexist/dir ]'; scp test.txt [email protected]:/tmp/nonexist/dir

怎么样,例如,
ssh [email protected] '[ -d /tmp/nonexist/dir ] || mkdir -p /tmp/nonexist/dir ]'; scp test.txt [email protected]:/tmp/nonexist/dir

回答by S0AndS0

sshfsbe fancy!

sshfs花哨!

Example .ssh/config

例子 .ssh/config

Host your-host
  HostHame example.com
  User name
  IdentitiesOnly yes
  IdentityFile ~/.ssh/private_key

Local setup aside from above only requires a targetmount point...

除了上面的本地设置只需要一个目标挂载点...

sudo mkdir /media/your-host
sudo chown ${USER}:${USER} /media/your-host

... after which things like mounting and un-mounting are far simpler to script.

...之后,诸如安装和卸载之类的事情脚本编写起来要简单得多。

Mount

sshfs your-host:within-home/some-dir /media/your-host

Unmount

卸载

fusermount -u /media/your-host

Best part about this approach, when a server allows it, is that locally running scripts can interact with the remote file system. Meaning that things like...

这种方法最好的部分是,当服务器允许时,本地运行的脚本可以与远程文件系统交互。这意味着诸如...

if ! [ -d "/media/your-host/nowhere" ]; then
  mkdir -vp "/media/your-host/nowhere"
fi

... become possible among many other tricks that can be preformed via such mounting magics.

......通过这种安装魔法可以执行的许多其他技巧成为可能。