tags:

views:

376

answers:

1

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")
A: 

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
Cheng
You are correct. SFTP actually doesnt follow FTP in allowing you to change the remote directory. If memory serves me right , SFTP always drops you into the SSH root directory and your stuck there. Sending a remote CD seems to be the only way to move.
Andrew Keith