Skip to content

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

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
See also #mtime .mkfifo .new .lutime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0