Ruby: Finding most recently modified file
Dir.glob("*").max_by {|f| File.mtime(f)}
Ruby - Get the second most recent file from a directory?
You can do as below using Ruby 2.2.0, which added an optional argument to the methods Enumerable#max_by
, Enumerable#min_by
and Enumerable#min
etc.
Dir.glob("./logs/*").max_by(2) {|f| File.mtime(f)}
# gives first 2 maximun.
# If you want the second most recent
Dir.glob("./logs/*").max_by(2) {|f| File.mtime(f)}.last
max_by(n) {|obj| block } → obj
If the
n
argument is given, minimum n elements are returned as an array.
How to find most recently modified file in a remote directory (via ssh)?
This is less a Net::SSH question as it is "What command can I issue to find the most recently modified file?"
SSH connections can issue a command, so once you know what command to send, or execute, you're done. I'd look at:
ls -Alt path/to/files | sed -n '2p'
Fleshing out something more usable results in:
require 'net/ssh'
HOST = 'hostname.domain'
USER = 'user'
PASSWORD = "password"
output = Net::SSH.start(HOST, USER, :password => PASSWORD) { |ssh|
ssh.exec!('ls -alt . | grep pattern_to_find')
}
puts output
Which, after filling in the fields with the right values and running it, connected to one of my hosts at work and returned something like:
drwxr-xr-x 11 xxxxxxxxxxxx xxxxxxxxx 4096 Oct 2 16:20 development
If you have multiple hits you need to retrieve, either expand the pattern after grep
or discard the pipe to grep
and parse your resulting output in Ruby once the command returns. You can also discard the t
flag from ls
if you want to sort locally, though it's a better idea to offload as much of the processing to the far-side host, rather than have it return a huge glob of data and process it locally. The less you return, the faster your overall code will be.
How to get last modified file in a directory to pass to system commands using Ruby?
There's no need to shell out to ls
and parse its output at all. Ruby gives you standard library methods to fetch directory contents and examine file mtimes. Here's a ruby method to return the name of a file in a directory with the latest mtime.
def last_modified_in dir
Dir.glob( File.join( dir,'*' ) ).
select {|f| File.file? f }.
sort_by {|f| File.mtime f }.
last
end
irb> system 'mkdir -p /tmp/foo'
irb> system 'rm /tmp/foo/*'
irb> ('a'..'c').each { |f| system "touch /tmp/foo/#{f}"; sleep 1; }
irb> puts last_modified_in '/tmp/foo'
# => /tmp/foo/c
Find date file was last modified
You can pass it a string of the file name.
irb(main):001:0> File.mtime("Gemfile")
=> 2016-08-22 13:54:43 -0700
To reference files from within rails you can use Rails.root.join
:
gemfile = Rails.root.join("Gemfile")
=> #<Pathname:/Users/username/projects/appname/Gemfile>
File.mtime(gemfile)
=> 2016-08-22 13:54:43 -0700
The docs also mention you can pass it an IO object.
How do I find the files changed in a commit using Rugged?
You can use Rugged::Commit#diff
to get the changes between the commit and its first parent or another Rugged::Commit
or Rugged::Tree
.
Related Topics
In Ruby, What Structures Can a 'Rescue' Statement Be Nested In
What Does ':Location => ...' and 'Head :Ok' Mean in the 'Respond_To' Format Statement
How to Unit-Test a JSON Controller
Rails 3 - Restricting Formats for Action in Resource Routes
Undefined Method '>' for Nil:Nilclass <Nomethoderror>
Ruby 1.9, Force_Encoding, But Check
Ruby Greed Koan - How to Improve My If/Then Soup
Rails - Invalid Authenticity Token After Deploy
Is It Idiomatic Ruby to Add an Assert( ) Method to Ruby's Kernel Class
Reraise (Same Exception) After Catching an Exception in Ruby
What Is the Meaning of "H" in "<%=H [ ...] %>"
How to Describe an Enumeration Column in a Rails 3 Migration
Force Bundle Install to Use Https:// Instead of Git:// for Github-Based Gems
How to Find the Path a Ruby Gem Is Installed at (I.E. Gem.Lib_Path C.F. Gem.Bin_Path)
Export Content of a SQLite3 Table in CSV
Actioncontroller::Routingerror (No Route Matches [Put] ) for Ajax Call