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.

3 Answers 3

107

Use mtime:

File.mtime("testfile") => 2014-04-13 16:00:23 -0300 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Is mtime OS agnostic?
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.

Comments

0

If you have already open the file (like you did in your example) it might be a better idea to use the instance method File#mtime instead:

File.new('file_path').mtime 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.