Skip to content

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

instance method

FileUtils#link_entry

(::FileUtils::path src, ::FileUtils::path dest, ?::boolish dereference_root, ?::boolish remove_destination) -> void
link_entry(src, dest, dereference_root = false, remove_destination = false)

Creates hard links; returns nil.

Arguments src and dest should be interpretable as paths.

If src is the path to a file and dest does not exist, creates a hard link at dest pointing to src:

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

If src is the path to a directory and dest does not exist, recursively creates hard links at dest pointing to paths in src:

Example
FileUtils.mkdir_p(['src1/dir0', 'src1/dir1'])
src_file_paths = [
  'src1/dir0/t0.txt',
  'src1/dir0/t1.txt',
  'src1/dir1/t2.txt',
  'src1/dir1/t3.txt',
  ]
FileUtils.touch(src_file_paths)
File.directory?('dest1')        # => true
FileUtils.link_entry('src1', 'dest1')
File.file?('dest1/dir0/t0.txt') # => true
File.file?('dest1/dir0/t1.txt') # => true
File.file?('dest1/dir1/t2.txt') # => true
File.file?('dest1/dir1/t3.txt') # => true

Optional arguments:

  • dereference_root - dereferences src if it is a symbolic link (false by default).

  • remove_destination - removes dest before creating links (false by default).

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

Related: FileUtils.ln (has different options).

See also .link_entry #link #ln #install also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0