link
Create hard links between files
TLDR
Create a hard link from a new file to an existing file
SYNOPSIS
link OLDFILE NEWFILE
PARAMETERS
OLDFILE
The path to the existing file that you want to create a hard link to.
NEWFILE
The path where the new hard link will be created. This file must NOT exist.
DESCRIPTION
The link command is a fundamental Unix utility used to create a hard link to an existing file. A hard link is essentially an additional directory entry that points to the same inode (the data structure containing file attributes and disk block locations) as the original file. This means that both the old and new filenames refer to the exact same data on the disk. Modifying the content through one name will be reflected when accessing it via the other name. Deleting one name does not remove the file data until the last hard link to that inode is removed.
Unlike the more versatile ln command, link is a minimalist wrapper around the link(2) system call. Its primary purpose is to atomically create a new hard link. A key characteristic is its strict behavior: link will only succeed if the target file (NEWFILE) does not already exist. This prevents accidental overwriting of files, making it a safer, albeit less flexible, alternative for specific scripting needs where atomic creation and strict non-overwrite behavior are paramount. It operates solely on files within the same filesystem.
CAVEATS
- Hard links can only be created between files residing on the same filesystem. They cannot span different partitions or storage devices.
- The link command cannot create hard links to directories. This is a fundamental limitation of hard links for security and filesystem consistency reasons.
- If NEWFILE already exists, link will fail with an error ("File exists") and will not overwrite the existing file. This distinguishes it from commands like ln -f.
- It does not support any command-line options (like verbose output or force overwrite) found in the more feature-rich ln utility.
SYSTEM CALL WRAPPER
The link command is a direct wrapper around the link(2) system call. This means it offers a low-level interface to the kernel's linking functionality, providing atomic link creation without additional user-space logic or options.
HISTORY
The link command is a very old and basic utility, directly exposing the link(2) system call. Its simplicity reflects its origin in early Unix systems where utilities often directly mirrored system calls. While more complex and feature-rich tools like ln have since become prevalent for link management, link persists as a standard POSIX utility, valued for its strict, atomic behavior and direct system call interface. Its lack of options is a testament to its singular, focused purpose.
SEE ALSO
ln(1): Create links (hard or symbolic), unlink(1): Call the unlink system call to remove the specified file (used by rm), rm(1): Remove files or directories, stat(2): Get file status (system call often used to examine inode information), link(2): Make a new name for a file (the system call wrapped by the link command)