Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.mtime
(::path | ::IO file_name) -> ::Time
File.mtime(object) → time
file.c
View on GitHub ↗
static VALUE
rb_file_s_mtime(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
int e = errno;
FilePathValue(fname);
rb_syserr_fail_path(e, fname);
}
return stat_time(stat_mtimespec(&st));
}
Returns a new Time object containing the modification time for the given object, which may be a string path or an IO object; see Modification Time:
Example
# Create directory; directory mtime established. dirpath = 'doc/foo' # => "doc/foo" Dir.mkdir(dirpath) File.mtime(dirpath) # => 2026-09-19 09:01:30.045928322 -0500 # Create file therein; file mtime established, directory mtime updated. filepath = File.join(dirpath, 't.tmp') # => "doc/foo/t.tmp" File.write(filepath, 'foo') File.mtime(filepath) # => 2026-09-19 09:02:32.860803131 -0500 File.mtime(dirpath) # => 2026-09-19 09:02:32.860803131 -0500 # Modify file; file mtime updated, directory mtime unchanged. File.write(filepath, 'bar') File.mtime(filepath) # => 2026-09-19 09:03:29.875611413 -0500 File.mtime(dirpath) # => 2026-09-19 09:02:32.860803131 -0500 FileUtils.rm_rf(dirpath) # Clean up. File.mtime($stdout) # => 2026-09-19 09:27:52 -0500 $stdout.flush File.mtime($stdout) # => 2026-09-19 09:28:08 -0500