LinuxCommandLibrary

link

Create hard links between files

TLDR

Create a hard link from a new file to an existing file

$ link [path/to/existing_file] [path/to/new_file]
copy

SYNOPSIS

link EXISTING_FILE NEW_LINK

PARAMETERS

EXISTING_FILE
    The path to the existing file for which the new hard link will be created.
It can be a relative or absolute path.

NEW_LINK
    The path to the new hard link that will point to the existing file.
It can be a relative or absolute path. The link must not exist before creation.

DESCRIPTION

The link command creates a hard link, which is an additional name for an existing file. Unlike symbolic links, hard links share the same inode and therefore point to the same data on the disk. This means that if you modify the file through one hard link, the changes are immediately reflected when accessing it through any other hard link. Hard links cannot span across different file systems and cannot be created for directories (except by the system administrator in certain cases). The link command is relatively rarely used directly in modern shell scripting as the ln command provides a more versatile approach that includes both hard and symbolic links.

CAVEATS

Hard links cannot span across file systems. Creating a hard link to a directory is generally not allowed for unprivileged users. If the original file is deleted, the data persists as long as at least one hard link remains. The link command may not be available on all systems. The ln command is a superset that is almost always available.

EXIT STATUS

The link command returns 0 on success and a non-zero value on failure. Common reasons for failure include: the EXISTING_FILE doesn't exist, the user lacks permission to create the NEW_LINK, the NEW_LINK already exists, or the link is attempted across file systems.

EXAMPLE

link original.txt link.txt creates a hard link named link.txt to the file original.txt. Any changes to either file affect the other as they share the same inode.

HISTORY

The link command is a traditional Unix utility. It provides a low-level way to create hard links. Its usage has declined in favor of the more flexible ln command, which can create both hard and symbolic links. The link system call on which it is based has been a fundamental part of the Unix API since its inception.

SEE ALSO

ln(1), unlink(1), stat(1)

Copied to clipboard