Skip to content
class method

FileUtils.link

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

Alias of .ln

Creates hard links.

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

When src is the path to an existing file and dest is the path to a non-existent file, creates a hard link at dest pointing to src; returns zero:

Example
Dir.children('tmp0/')                    # => ["t.txt"]
Dir.children('tmp1/')                    # => []
FileUtils.ln('tmp0/t.txt', 'tmp1/t.lnk') # => 0
Dir.children('tmp1/')                    # => ["t.lnk"]

When src is the path to an existing file and dest is the path to an existing directory, creates a hard link at dest/src pointing to src; returns zero:

Example
Dir.children('tmp2')               # => ["t.dat"]
Dir.children('tmp3')               # => []
FileUtils.ln('tmp2/t.dat', 'tmp3') # => 0
Dir.children('tmp3')               # => ["t.dat"]

When src is an array of paths to existing files and dest is the path to an existing directory, then for each path target in src, creates a hard link at dest/target pointing to target; returns src:

Example
Dir.children('tmp4/')                               # => []
FileUtils.ln(['tmp0/t.txt', 'tmp2/t.dat'], 'tmp4/') # => ["tmp0/t.txt", "tmp2/t.dat"]
Dir.children('tmp4/')                               # => ["t.dat", "t.txt"]

Keyword arguments:

  • force: true - overwrites dest if it exists.

  • noop: true - does not create links.

  • verbose: true - prints an equivalent command:

    Example
    FileUtils.ln('tmp0/t.txt', 'tmp1/t.lnk', verbose: true)
    FileUtils.ln('tmp2/t.dat', 'tmp3', verbose: true)
    FileUtils.ln(['tmp0/t.txt', 'tmp2/t.dat'], 'tmp4/', verbose: true)
    

    Output:

    ln tmp0/t.txt tmp1/t.lnk
    ln tmp2/t.dat tmp3
    ln tmp0/t.txt tmp2/t.dat tmp4/

Raises an exception if dest is the path to an existing file and keyword argument force is not true.

Related: FileUtils.link_entry (has different options).

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