如果远程主机上不存在 ssh 的目录,如何在该目录上创建该目录?

发布于 2024-08-02 14:36:42 字数 498 浏览 10 评论 0原文

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

function upload {
    scp $1 [email protected]:$2
}

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

我不想每次都通过 ssh 登录并创建目录,但如果我别无选择,那么我别无选择。

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 is the folder I want it to be placed in on the remote server:

function upload {
    scp $1 [email protected]:$2
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

随梦而飞# 2024-08-09 14:36:42

您可以使用rsync

例如,

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

关于 rsync 的注释:

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

You can use rsync.

For example,

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

Note about rsync:

rsync is 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.

呆萌少年 2024-08-09 14:36:42

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

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

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

其中

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

如果目录已存在,mkdir 会抱怨但文件仍然被复制过来。 现有目录不会被覆盖。如果该目录不存在,mkdir 将创建它。

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.

微暖i 2024-08-09 14:36:42

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

有点尴尬,但它可以完成工作。

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.

Kind of awkward, but it would do the job.

瑶笙 2024-08-09 14:36:42

这是一个两步过程,

ssh [email protected] "mkdir -p $2"

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

scp $1 [email protected]:$2

This is a two step process

ssh [email protected] "mkdir -p $2"

This ensures directory structure is created. Then, you copy

scp $1 [email protected]:$2
当爱已成负担 2024-08-09 14:36:42

比如说
ssh [电子邮件受保护] '[ -d /tmp/nonexist /目录] || mkdir -p /tmp/nonexist/dir ]'; scp test.txt [电子邮件受保护]:/tmp/nonexist/dir< /代码>

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

花桑 2024-08-09 14:36:42

sshfs 太棒了!

示例.ssh/config

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

除了上述之外,本地设置只需要一个目标安装点......

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

之后诸如安装和取消之类的事情-mounting 的脚本要简单得多。

装载

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

卸载

fusermount -u /media/your-host

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

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

在许多其他可以通过这种安装魔法执行的技巧中变得可能。

sshfs be fancy!

Example .ssh/config

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

Local setup aside from above only requires a target mount 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

The 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 performed via such mounting magics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文