Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.utime
(::Time | ::Numeric atime, ::Time | ::Numeric mtime, *::path file_name) -> ::Integer
File.utime(atime, mtime, *paths) → integer
file.c
View on GitHub ↗
static VALUE
rb_file_s_utime(int argc, VALUE *argv, VALUE _)
{
return utime_internal_i(argc, argv, FALSE);
}
For the entry at the paths in paths, updates its access time to the given atime and its modification time to the given mtime; see Filesystem Timestamps. Returns the number of entries updated. Each path points to a file or directory.
Each given time may be a Time object, an integer representing a time, or nil (meaning Time.now):
Example
filepath = '/tmp/t.tmp' File.write(filepath, 'foo') File.atime(filepath) # => 2026-09-29 12:38:29.889703781 -0500 File.mtime(filepath) # => 2026-09-29 12:38:29.889703781 -0500 time = Time.now File.utime(time, time, filepath) File.atime(filepath) # => 2026-09-29 12:38:52.533160634 -0500 File.mtime(filepath) # => 2026-09-29 12:38:52.533160634 -0500 File.utime(0, 0, filepath) File.atime(filepath) # => 1969-12-31 18:00:00 -0600 File.mtime(filepath) # => 1969-12-31 18:00:00 -0600 File.utime(nil, nil, filepath) File.atime(filepath) # => 2026-09-29 12:39:50.859421265 -0500 File.mtime(filepath) # => 2026-09-29 12:39:50.859421265 -0500 File.delete(filepath) # Clean up.
Raises an exception if any entry cannot be updated; some entries may have already been updated.
Follows symbolic links; use File.lutime to update the times for symbolic links.