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

时间:2022-06-02 00:13:11

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 user@ftpserver.com:/data/install/somefolder

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

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

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

注:我已阅读有关rsync的内容,但我不确定它是如何工作的或如何使用它。

2 个解决方案

#1


34  

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:

ssh user@ftpserver.com 'mkdir -p /data/install/somefolder'

2. Copy to it:

scp -r /data/install/somefolder user@ftpserver.com:/data/install/somefolder

Put them together

server="user@ftpserver.com"
destiny="/data/install/somefolder"
src="/data/install/somefolder"
ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny"

#2


20  

Short answer: no.

简答:不。

...but rsync does, which is why i have aliased scp to rsync -Pravdtze ssh on 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. Manpage is a good place to start, but there are a lot of info easily available. Here's a decent list of examples.

...但rsync确实如此,这就是为什么我在我的盒子上将scp别名为rsync -Pravdtze ssh。是的,这是很多开关,它们组合起来产生了我喜欢的rsync行为。由于rsync确实提供了一套非常广泛的开关和选项,我建议你做一些研究,看看哪种最适合你的需求。 Manpage是一个很好的起点,但有很多信息很容易获得。这是一个很好的例子列表。

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,那么它将失败。

#1


34  

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:

ssh user@ftpserver.com 'mkdir -p /data/install/somefolder'

2. Copy to it:

scp -r /data/install/somefolder user@ftpserver.com:/data/install/somefolder

Put them together

server="user@ftpserver.com"
destiny="/data/install/somefolder"
src="/data/install/somefolder"
ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny"

#2


20  

Short answer: no.

简答:不。

...but rsync does, which is why i have aliased scp to rsync -Pravdtze ssh on 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. Manpage is a good place to start, but there are a lot of info easily available. Here's a decent list of examples.

...但rsync确实如此,这就是为什么我在我的盒子上将scp别名为rsync -Pravdtze ssh。是的,这是很多开关,它们组合起来产生了我喜欢的rsync行为。由于rsync确实提供了一套非常广泛的开关和选项,我建议你做一些研究,看看哪种最适合你的需求。 Manpage是一个很好的起点,但有很多信息很容易获得。这是一个很好的例子列表。

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,那么它将失败。