LINK(2)
β¨ NAME β¨
link, linkat β make a hard file link ππ
okay so hi bestie π«Ά this is the LINK(2) page out of the System Calls Manual, and we're gonna talk about hard links, which are genuinely one of the most iconic little tricks in all of Unix. no cap. let's get into it π
π LIBRARY π
Standard C Library (libc, -lc) π
πΈ SYNOPSIS πΈ
#include <unistd.h>
int
link(const char *name1, const char *name2);
int
linkat(int fd1, const char *name1, int fd2, const char *name2,
int flag);
π DESCRIPTION π
The link() system call atomically creates the specified directory entry (hard link) name2 with the attributes of the underlying object pointed at by name1. and if that link goes through: the link count of the underlying object gets incremented, and name1 and name2 share equal access and rights to the underlying object. it's giving shared custody but make it filesystem π«Ά
If name1 is removed, the file name2 is not deleted and the link count of the underlying object is decremented. she's still standing, periodt. π
The object pointed at by the name1 argument must exist for the hard link to succeed, and both name1 and name2 must be in the same file system. and name1 may not be a directory. those are the rules, i don't make them, the kernel does π
The linkat() system call is equivalent to link except in the case where either name1 or name2 or both are relative paths. in that case a relative path name1 is interpreted relative to the directory associated with the file descriptor fd1 instead of the current working directory, and similarly for name2 and the file descriptor fd2. so basically: you get to pick the starting point. main character energy β¨
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
AT_SYMLINK_FOLLOWβ ifname1names a symbolic link, a new link for the target of the symbolic link is created. (so you're linking the thing it points at, not the symlink itself π¦)
If linkat() is passed the special value AT_FDCWD in the fd1 or fd2 parameter, the current working directory is used for the respective name argument. If both fd1 and fd2 have value AT_FDCWD, the behavior is identical to a call to link(). Unless flag contains the AT_SYMLINK_FOLLOW flag, if name1 names a symbolic link, a new link is created for the symbolic link name1 and not its target. sneaky little default, iykyk π
π RETURN VALUES π
The link() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. zero is a slay, negative one is a vibe check fail π
π ERRORS π
The link() system call will fail and no link will be created if:
[ENOTDIR]β a component of either path prefix is not a directory.[ENAMETOOLONG]β a component of either pathname exceeded 255 characters, or entire length of either path name exceeded 1023 characters.[ENOENT]β a component of either path prefix does not exist.[EOPNOTSUPP]β the file system containing the file named byname1does not support links.[EMLINK]β the link count of the file named byname1would exceed 32767.[EACCES]β a component of either path prefix denies search permission.[EACCES]β the requested link requires writing in a directory with a mode that denies write permission.[ELOOP]β too many symbolic links were encountered in translating one of the pathnames.[ENOENT]β the file named byname1does not exist.[EEXIST]β the link named byname2does exist.[EPERM]β the file named byname1is a directory.[EPERM]β the file named byname1has its immutable or append-only flag set, see thechflags(2)manual page for more information.[EPERM]β the parent directory of the file named byname2has its immutable flag set.[EXDEV]β the link named byname2and the file named byname1are on different file systems.[ENOSPC]β the directory in which the entry for the new link is being placed cannot be extended because there is no space left on the file system containing the directory.[EDQUOT]β the directory in which the entry for the new link is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted.[EIO]β an I/O error occurred while reading from or writing to the file system to make the directory entry.[EINTEGRITY]β corrupted data was detected while reading from the file system.[EROFS]β the requested link requires writing in a directory on a read-only file system.[EFAULT]β one of the pathnames specified is outside the process's allocated address space.
In addition to the errors returned by the link(), the linkat() system call may fail if:
[EBADF]β thename1orname2argument does not specify an absolute path and thefd1orfd2argument, respectively, is neitherAT_FDCWDnor a valid file descriptor open for searching.[EINVAL]β the value of theflagargument is not valid.[ENOTDIR]β thename1orname2argument is not an absolute path andfd1orfd2, respectively, is neitherAT_FDCWDnor a file descriptor associated with a directory.
π¦ SEE ALSO π¦
chflags(2), readlink(2), symlink(2), unlink(2)
go read those too, they're all part of the same friend group fr fr π
π STANDARDS π
The link() system call is expected to conform to ISO/IEC 9945-1:1990 (βPOSIX.1β). The linkat() system call follows The Open Group Extended API Set 2 specification. certified and standardized, as she should be π
β HISTORY β
The link() function appeared in Version 1 AT&T UNIX. The linkat() system call appeared in FreeBSD 8.0. ancient lineage honestly, she's been iconic since forever.
The link() system call traditionally allows the super-user to link directories which corrupts the file system coherency. This implementation no longer permits it. yeah that was a mess, we don't do that anymore π
okay that's the whole page bestie, you're officially fluent in hard links now πβ¨
and that's a wrap on the LINK(2) page from Debian, dated March 30, 2020 π bye bestie, go make some links π«Άπ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms