Method: FileUtils.cp_lr

Defined in:
lib/fileutils.rb

.cp_lr(src, dest, noop: nil, verbose: nil, dereference_root: true, remove_destination: false) ⇒ Object

Creates hard links.

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 directory and dest does not exist, creates links dest and descendents pointing to src and its descendents:

tree('src0')
# => src0
#    |-- sub0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- sub1
#        |-- src2.txt
#        `-- src3.txt
File.exist?('dest0') # => false
FileUtils.cp_lr('src0', 'dest0')
tree('dest0')
# => dest0
#    |-- sub0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- sub1
#        |-- src2.txt
#        `-- src3.txt

If src and dest are both paths to directories, creates links dest/src and descendents pointing to src and its descendents:

tree('src1')
# => src1
#    |-- sub0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- sub1
#        |-- src2.txt
#        `-- src3.txt
FileUtils.mkdir('dest1')
FileUtils.cp_lr('src1', 'dest1')
tree('dest1')
# => dest1
#    `-- src1
#        |-- sub0
#        |   |-- src0.txt
#        |   `-- src1.txt
#        `-- sub1
#            |-- src2.txt
#            `-- src3.txt

If src is an array of paths to entries and dest is the path to a directory, for each path filepath in src, creates a link at dest/filepath pointing to that path:

tree('src2')
# => src2
#    |-- sub0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- sub1
#        |-- src2.txt
#        `-- src3.txt
FileUtils.mkdir('dest2')
FileUtils.cp_lr(['src2/sub0', 'src2/sub1'], 'dest2')
tree('dest2')
# => dest2
#    |-- sub0
#    |   |-- src0.txt
#    |   `-- src1.txt
#    `-- sub1
#        |-- src2.txt
#        `-- src3.txt

Keyword arguments:

  • dereference_root: false - if src is a symbolic link, does not dereference it.

  • noop: true - does not create links.

  • remove_destination: true - removes dest before creating links.

  • verbose: true - prints an equivalent command:

    FileUtils.cp_lr('src0', 'dest0', noop: true, verbose: true)
    FileUtils.cp_lr('src1', 'dest1', noop: true, verbose: true)
    FileUtils.cp_lr(['src2/sub0', 'src2/sub1'], 'dest2', noop: true, verbose: true)
    

    Output:

    cp -lr src0 dest0
    cp -lr src1 dest1
    cp -lr src2/sub0 src2/sub1 dest2
    

Raises an exception if dest is the path to an existing file or directory and keyword argument remove_destination: true is not given.

Related: methods for copying.



628
629
630
631
632
633
634
635
# File 'lib/fileutils.rb', line 628

def cp_lr(src, dest, noop: nil, verbose: nil,
          dereference_root: true, remove_destination: false)
  fu_output_message "cp -lr#{remove_destination ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if verbose
  return if noop
  fu_each_src_dest(src, dest) do |s, d|
    link_entry s, d, dereference_root, remove_destination
  end
end