2015-05-14 120 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