2015-05-14 111 views
2

假设您将文件夹f_1从本地机器复制到目标机器m_1并将其作为mf_1复制到/tmp目录中。net :: scp复制/覆盖文件夹

console: 
[[email protected]_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: 
[[email protected]_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: 
[[email protected]_1 tmp] ls -a | grep mf_1 # => folder exists 
[[email protected]_1 tmp] cd mf_1 
[[email protected]_1 m_f1] ls # => f_1 => /tmp/mf_1/f_1 

所以,与其mf_1不被覆盖夹复制的/tmp/mf_1内,造成/tmp/mf_1/f_1

的问题是非常简单的,如何保留行为,因此它是一致的,并呼吁

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

连续两次将采取同样的方式既当文件夹是否存在不?

+0

您应该考虑使用SFTP而不是SCP如果你能。 – Kenster

+0

@Kenster,你能举个例子吗? – ted

回答

1

如果source是一个dir,我最终添加了一个点。
这不是理想的,这里有一个例子:

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