net::scp copy/overwriting 文件夹

net::scp copy/overwriting folder

假设您将文件夹 f_1 从本地计算机复制到目标计算机 m_1/tmp 目录中 mf_1

console:
[root@m_1 tmp] ls -a | grep mf_1 # => doesn't exist

irb:
options = {recursive: true}
Net::SCP.upload!(host, user, '~/f_1', '/tmp/mf_1', options)

console:
[root@m_1 tmp] ls -a | grep mf_1 # => folder exists, everything is fine

# but then, if you try to overwrite the existing folder...
irb:
Net::SCP.upload!(host, user, '~/f_1', '/tmp/mf_1', options)

console:
[root@m_1 tmp] ls -a | grep mf_1 # => folder exists
[root@m_1 tmp] cd mf_1
[root@m_1 m_f1] ls # => f_1 => /tmp/mf_1/f_1

因此,文件夹被复制到 /tmp/mf_1 中,而不是 mf_1 被覆盖,导致 /tmp/mf_1/f_1

问题很简单,如何保持行为一致并调用

Net::SCP.upload!(host, user, '~/f_1', '/tmp/mf_1', options)

当文件夹存在和不存在时,连续两次会以相同的方式操作吗?

如果 source 是目录,我最后添加了一个点。
这并不理想,这里有一个例子:

options = {recursive: true}

# target /tmp/mf_1 doesn't exist
Net::SCP.upload!(host, user, '~/f_1/.', '/tmp/mf_1', options)
# target /tmp/mf_1 has been created

# second time
Net::SCP.upload!(host, user, '~/f_1/.', '/tmp/mf_1', options)
# target /tmp/mf_1 has been overwritten
# not dir, but files in it, which is what we usually want