开发者

Is there any way to get the date modified from Net::SSH or NET::SFTP commands in ruby?

开发者 https://www.devze.com 2022-12-27 08:39 出处:网络
Is there an easy way to get the date modified of a file by using Net::SFTP? It\'d be nice to be able to do this:

Is there an easy way to get the date modified of a file by using Net::SFTP?

It'd be nice to be able to do this:

Net::SFTP.start('some_server') do |sftp|
  sftp.dir.glob('*').each do |file|
    puts file.mtime
  开发者_如何学运维end
end

But that's not possible (to my knowledge).

Berns.


Your example code is almost there, you just need to use file.attributes.mtime where you had file.mtime.

Also, I'm guessing the code in the question was just an example, but in order for it to execute you also need to pass the username and password to start and pass the path as well as the pattern to glob. So a working example would be:

Net::SFTP.start('some_server', 'mike', :password => 'secret') do |sftp|
  sftp.dir.glob('.', '*').each do |file|
    puts file.attributes.mtime
  end
end

The value returned by mtime will be the number of seconds since the epoch so you may want to pass it to Time.at to convert it to a Time object.

In case you're curious, the other attributes available in the same way are:

  • permissions
  • uid
  • gid
  • size
  • atime (time of last access)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号