62

Is it possible to read a file's modification date with Ruby? I have successfully opened a text file and captured the contents of the file with

File.open("test.txt", "r").each do |line|"

but it would be very useful to read the modification date of the file.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
ryan1393402
  • 715
  • 1
  • 6
  • 9

2 Answers2

104

Use mtime:

File.mtime("testfile")
=> 2014-04-13 16:00:23 -0300
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Constantine M
  • 1,507
  • 2
  • 11
  • 8
27

"Returns the modification time for the named file as a Time object."

File.mtime("testfile")   #=> Tue Apr 08 12:58:04 CDT 2003

Check this.

Matzi
  • 13,770
  • 4
  • 33
  • 50