Linux 如果目标文件夹不存在,scp 是否创建目标文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12920947/
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-08-06 14:37:20  来源:igfitidea点击:

Does scp create the target folder if it does not exist

linuxbashscp

提问by Benoit Paquette

I am wondering if scp will create the target folder if it does not exist on the remote server. For example, would this work?

我想知道如果远程服务器上不存在 scp 是否会创建目标文件夹。例如,这行得通吗?

scp -r /data/install/somefolder [email protected]:/data/install/somefolder

Here the folder /data/install/somefolderdoesn't exist on ftp server, so would this command create it?

这里的文件夹/data/install/somefolder在 ftp 服务器上不存在,所以这个命令会创建它吗?

N.B. I have read about rsync but am not really sure how it works or how to use it yet.

注意我已经阅读了 rsync 但我不确定它是如何工作的或如何使用它。

回答by Jarmund

Short answer: no.

简短的回答:没有。

...but rsync does, which is why I have aliased scpto rsync -Pravdtze sshon my box. Yes, that's a lot of switches, which in combination produces my preferred rsync behavior. As rsync does provide a very extensive set of switches and options, I suggest you do some research on it to see what fits your needs best. Man-page is a good place to start, but there are a lot of info easily available. Here's a decent list of examples.

...但确实rsync的,这就是我为什么别名scprsync -Pravdtze ssh对我的盒子。是的,这是很多开关,它们结合起来产生了我喜欢的 rsync 行为。由于 rsync 确实提供了一组非常广泛的开关和选项,我建议您对其进行一些研究,看看哪些最适合您的需求。手册页是一个很好的起点,但有很多信息很容易获得。这是一个不错的示例列表

Edit:Actually, in that particular case you posted, the folder will be created, as that's the folder you're copying. However, if you're trying to copy it to user@remotehost:somenonexistentfolder/somefolder, then it will fail.

编辑:实际上,在您发布的特定情况下,将创建文件夹,因为这是您要复制的文件夹。但是,如果您尝试将其复制到user@remotehost:somenonexistentfolder/somefolder,则会失败。

回答by Thamme Gowda

To achieve the task with ssh and scp (instead of rsync):

要使用 ssh 和 scp(而不是 rsync)完成任务:

Solution

解决方案

Lets break into 2 steps :

让我们分成两个步骤:

1. Create directory if missing:

1.如果没有创建目录:

ssh [email protected] "mkdir -p /data/install/somefolder"

2. Copy to it:

2.复制到它:

scp -r /data/install/somefolder [email protected]:/data/install/somefolder


Put them together

把它们放在一起

server="[email protected]"
destiny="/data/install/somefolder"
src="/data/install/somefolder"
ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny"