Skip to content
instance method

FileUtils#cp

(::FileUtils::pathlist src, ::FileUtils::path dest, ?preserve: ::boolish, ?noop: ::boolish, ?verbose: ::boolish) -> void
cp(src, dest, preserve: nil, noop: nil, verbose: nil)

Copies files.

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

If src is the path to a file and dest is not the path to a directory, copies src to dest:

Example
FileUtils.touch('src0.txt')
File.exist?('dest0.txt') # => false
FileUtils.cp('src0.txt', 'dest0.txt')
File.file?('dest0.txt')  # => true

If src is the path to a file and dest is the path to a directory, copies src to dest/src:

Example
FileUtils.touch('src1.txt')
FileUtils.mkdir('dest1')
FileUtils.cp('src1.txt', 'dest1')
File.file?('dest1/src1.txt') # => true

If src is an array of paths to files and dest is the path to a directory, copies from each src to dest:

Example
src_file_paths = ['src2.txt', 'src2.dat']
FileUtils.touch(src_file_paths)
FileUtils.mkdir('dest2')
FileUtils.cp(src_file_paths, 'dest2')
File.file?('dest2/src2.txt') # => true
File.file?('dest2/src2.dat') # => true

Keyword arguments:

  • preserve: true - preserves file times.

  • noop: true - does not copy files.

  • verbose: true - prints an equivalent command:

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

    Output:

    Example
    cp src0.txt dest0.txt
    cp src1.txt dest1
    cp src2.txt src2.dat dest2
    

Raises an exception if src is a directory.

Related: methods for copying.

See also .cp #copy_stream #cp_lr #copy_file also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0