Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
FileUtils.copy_entry
# File tmp/rubies/ruby-4.1.0-preview1/lib/fileutils.rb, line 1066
def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
if dereference_root
src = File.realpath(src)
end
Entry_.new(src, nil, false).wrap_traverse(proc do |ent|
destent = Entry_.new(dest, ent.rel, false)
File.unlink destent.path if remove_destination && (File.file?(destent.path) || File.symlink?(destent.path))
ent.copy destent.path
end, proc do |ent|
destent = Entry_.new(dest, ent.rel, false)
ent.copy_metadata destent.path if preserve
end)
end
Recursively copies files from src to dest.
Arguments src and dest should be interpretable as paths.
If src is the path to a file, copies src to dest:
FileUtils.touch('src0.txt') File.exist?('dest0.txt') # => false FileUtils.copy_entry('src0.txt', 'dest0.txt') File.file?('dest0.txt') # => true
If src is a directory, recursively copies src to dest:
tree('src1') # => src1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt FileUtils.copy_entry('src1', 'dest1') tree('dest1') # => dest1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt
The recursive copying preserves file types for regular files, directories, and symbolic links; other file types (FIFO streams, device files, etc.) are not supported.
Optional arguments:
-
dereference_root- ifsrcis a symbolic link, follows the link (falseby default). -
preserve- preserves file times (falseby default). -
remove_destination- removesdestbefore copying files (falseby default).
Related: methods for copying.