How to enter a remote directory on Ruby NET:SFTP?
Question
How to enter a directory like the command - cd, thus operate remote files without a path prefix ?
Here is my current code.
Net::SFTP.start do |sftp|
sftp.rename!(REMOTE_PATH + "latest.zip", REMOTE_PATH + "latest.back.zip")
sftp.upload!("latest.zip", REMOTE_PATH + "latest.zip")
end
I would like to have:
sftp.cd REMOTE_PATH
sftp.rename!("latest.zip", "latest.back.zip")
sftp.upload!("latest.zip", "latest.zip")
Solution
I found a solution, which is use the SSH connect instead.. Not works. SFTP path seems irrelevant to SSH path. Let me know if you have other options.
Net::SSH.start("localhost", "user", "password") do |ssh|
ssh.sftp.upload!("/local/file.tgz", "/remote/file.tgz")
ssh.exec! "cd /some/path && tar xf /remote/file.tgz && rm /remote/file.tgz"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow