Skip to content

Instantly share code, notes, and snippets.

@konung
Last active December 19, 2015 08:09
Show Gist options
  • Save konung/5923387 to your computer and use it in GitHub Desktop.
Save konung/5923387 to your computer and use it in GitHub Desktop.
Find most recently modified ( last modified) file or directory using Ruby's FileUtils. Extends FileUtils
module FileUtils
def sorted_by_modified_files(path='.',extension_filter = "")
Dir.open(path).entries.map do |f|
[f,File.mtime(File.join(path,f))]
end.sort_by do |a|
a[1]
end.delete_if do |a|
File.stat(File.join(path,a[0])).directory? or
a[0] == '.' or a[0] == '..' or a[0].match(/#{extension_filter}$/).nil?
end
end
def sorted_by_modified_dirs(path='.')
Dir.open(path).entries.map do |f|
[f,File.mtime(File.join(path,f))]
end.sort_by do |a|
a[1]
end.delete_if do |a|
!(File.stat(File.join(path,a[0])).directory?) or a[0] == '.' or a[0] == '..'
end
end
def last_modified_file(path='.',extension_filter = "")
sorted_by_modified_files(path, extension_filter).last[0]
end
def last_modified_dir(path='.')
sorted_by_modified_dirs(path).last[0]
end
end
include FileUtils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment