Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

net::scp copy/overwriting folder

Tags:

scp

ssh

ruby

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?

like image 759
ted Avatar asked Jan 31 '26 09:01

ted


1 Answers

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
like image 81
ted Avatar answered Feb 02 '26 09:02

ted



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!