ted
ted

Reputation: 5329

net::scp copy/overwriting folder

Say you copy folder f_1 from your local machine to target machine m_1 into the /tmp directory as 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

So, instead of mf_1 being overwritten folder was copied inside of /tmp/mf_1, resulting in /tmp/mf_1/f_1.

The question is pretty simple, how to preserve the behavior so it's consistent and calling

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

twice in a row would act the same way both when folder exists and doesn't?

Upvotes: 3

Views: 1095

Answers (1)

ted
ted

Reputation: 5329

I ended up adding a dot, if source is a dir.
This is not ideal, here's an example:

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

Upvotes: 1

Related Questions