Skip to content
class method

FileUtils.touch

(::FileUtils::pathlist list, ?noop: ::boolish, ?verbose: ::boolish, ?mtime: (::Time | ::Numeric)?, ?nocreate: ::boolish) -> void
touch(list, noop: nil, verbose: nil, mtime: nil, nocreate: nil)

Updates modification times (mtime) and access times (atime) of the entries given by the paths in list (a single path or an array of paths); returns list if it is an array, [list] otherwise.

By default, creates an empty file for any path to a non-existent entry; use keyword argument nocreate to raise an exception instead.

Argument list or its elements should be interpretable as paths.

Examples:

Example
# Single path.
f = File.new('src0.txt') # Existing file.
f.atime # => 2022-06-10 11:11:21.200277 -0700
f.mtime # => 2022-06-10 11:11:21.200277 -0700
FileUtils.touch('src0.txt')
f = File.new('src0.txt')
f.atime # => 2022-06-11 08:28:09.8185343 -0700
f.mtime # => 2022-06-11 08:28:09.8185343 -0700

# Array of paths.
FileUtils.touch(['src0.txt', 'src0.dat'])

Keyword arguments:

  • mtime: <i>time</i> - sets the entry’s mtime to the given time, instead of the current time.

  • nocreate: true - raises an exception if the entry does not exist.

  • noop: true - does not touch entries; returns nil.

  • verbose: true - prints an equivalent command:

    Example
    FileUtils.touch('src0.txt', noop: true, verbose: true)
    FileUtils.touch(['src0.txt', 'src0.dat'], noop: true, verbose: true)
    FileUtils.touch(path, noop: true, verbose: true)
    

    Output:

    Example
    touch src0.txt
    touch src0.txt src0.dat
    touch src0.txt
    

Related: FileUtils.uptodate?.

See also #touch .symlink .uptodate? .safe_unlink also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0