Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

FileUtils.install

(::FileUtils::path src, ::FileUtils::path dest, ?mode: ::FileUtils::mode?, ?owner: ::String?, ?group: ::String?, ?preserve: ::boolish, ?noop: ::boolish, ?verbose: ::boolish) -> void
install(src, dest, mode: nil, owner: nil, group: nil, preserve: nil, noop: nil, verbose: nil)

Copies a file entry. See install(1).

Arguments src (a single path or an array of paths) and dest (a single path) should be interpretable as paths;

If the entry at dest does not exist, copies from src to dest:

Example
File.read('src0.txt')    # => "aaa\n"
File.exist?('dest0.txt') # => false
FileUtils.install('src0.txt', 'dest0.txt')
File.read('dest0.txt')   # => "aaa\n"

If dest is a file entry, copies from src to dest, overwriting:

Example
File.read('src1.txt')  # => "aaa\n"
File.read('dest1.txt') # => "bbb\n"
FileUtils.install('src1.txt', 'dest1.txt')
File.read('dest1.txt') # => "aaa\n"

If dest is a directory entry, copies from src to dest/src, overwriting if necessary:

Example
File.read('src2.txt')       # => "aaa\n"
File.read('dest2/src2.txt') # => "bbb\n"
FileUtils.install('src2.txt', 'dest2')
File.read('dest2/src2.txt') # => "aaa\n"

If src is an array of paths and dest points to a directory, copies each path path in src to dest/path:

Example
File.file?('src3.txt') # => true
File.file?('src3.dat') # => true
FileUtils.mkdir('dest3')
FileUtils.install(['src3.txt', 'src3.dat'], 'dest3')
File.file?('dest3/src3.txt') # => true
File.file?('dest3/src3.dat') # => true

Keyword arguments:

  • group: <i>group</i> - changes the group if not nil, using File.chown.

  • mode: <i>permissions</i> - changes the permissions. using File.chmod.

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

  • owner: <i>owner</i> - changes the owner if not nil, using File.chown.

  • preserve: true - preserve timestamps using File.utime.

  • verbose: true - prints an equivalent command:

    Example
    FileUtils.install('src0.txt', 'dest0.txt', noop: true, verbose: true)
    FileUtils.install('src1.txt', 'dest1.txt', noop: true, verbose: true)
    FileUtils.install('src2.txt', 'dest2', noop: true, verbose: true)
    

    Output:

    install -c src0.txt dest0.txt
    install -c src1.txt dest1.txt
    install -c src2.txt dest2

Related: methods for copying.

See also #install .identical? .link .have_option? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0