Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

FileUtils#link_entry

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

Keyword arguments:

  • dereference_root: true - dereferences src if it is a symbolic link.

  • remove_destination: true - removes dest before creating links.

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

Related: FileUtils.ln (has different options).

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