简体   繁体   English

有没有办法在ruby中从Net :: SSH或NET :: SFTP命令修改日期?

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

Is there an easy way to get the date modified of a file by using Net::SFTP? 有没有一种简单的方法来使用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 . 您的示例代码几乎就在那里,您只需要使用file.attributes.mtime ,其中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 . 此外,我猜测问题中的代码只是一个示例,但为了执行它,您还需要传递用户名和密码来start并将路径以及模式传递给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. mtime返回的值将是自纪元以来的秒数,因此您可能希望将其传递给Time.at以将其转换为Time对象。

In case you're curious, the other attributes available in the same way are: 如果你很好奇,以同样的方式提供的其他属性是:

  • permissions
  • uid
  • gid
  • size
  • atime (time of last access) atime (最后访问时间)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM