Skip to Content
gnux

linkx

Call the link function to create a hard link to a file.

Mirrors GNU link. Run it as linkx, or as gnux linkx.

Synopsis

linkx FILE1 FILE2

GNU reference

Description

linkx creates FILE2 as a hard link to the existing FILE1: a second name for the same file, sharing its contents and metadata. It is the bare link(2) system call, with no options: it never overwrites FILE2, never makes a symbolic link, and never places the link inside a directory. Exactly two operands are required. lnx does the same with options; linkx is for scripts that want exactly one system call and its error.

Permissions

Needs write and search permission on the directory that will hold FILE2. FILE1 and FILE2 must be on the same filesystem, and most systems refuse a hard link to a directory. On Windows, hard links need an NTFS volume.

Use cases

  • Giving a file a second name without copying its data.
  • Creating a hard link in a script that must fail rather than overwrite.
  • Keeping a snapshot name for a file that is about to be renamed or replaced.
  • Testing whether two paths are on the same filesystem (the link fails if not).
  • Implementing a simple lock: link succeeds for exactly one of several racing processes.

Flags

      --help      display this help and exit
      --version   output version information and exit

Examples

Give a file a second name

linkx report.txt report-final.txt

Refuse to overwrite an existing name (exit status 1)

linkx a.txt existing.txt || echox 'already there'

Check that both names are the same file (same inode in the first column)

linkx data.bin data.bak && lsx -i data.bin data.bak

Keep the old contents reachable before replacing a file

linkx config.yaml config.yaml.old && cpx new.yaml config.yaml

Take a lock that only one process can get

touchx lock.$$ && linkx lock.$$ lock && echox 'got the lock'

Release that lock

unlinkx lock
linkx build/app dist/app

Count a file's names after linking it

linkx a b && lsx -l a
linkx -- -file copy
gnux shell -c 'linkx notes.md notes-link.md'

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

linkx --help                # options, first examples, exit status
gnux help linkx             # this page: the full manual
gnux help linkx --OPTION    # one option in depth, with its examples

See also

On this page