1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2006, 2014 Michael Kerrisk
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
8 .\" Modified 1994-08-21 by Michael Haardt
9 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Modified 2005-04-04, as per suggestion by Michael Hardt for rename.2
12 .TH link 2 (date) "Linux man-pages (unreleased)"
14 link, linkat \- make a new name for a file
17 .RI ( libc ", " \-lc )
20 .B #include <unistd.h>
22 .BI "int link(const char *" oldpath ", const char *" newpath );
24 .BR "#include <fcntl.h> " "/* Definition of " AT_* " constants */"
25 .B #include <unistd.h>
27 .BI "int linkat(int " olddirfd ", const char *" oldpath ,
28 .BI " int " newdirfd ", const char *" newpath ", int " flags );
32 Feature Test Macro Requirements for glibc (see
33 .BR feature_test_macros (7)):
39 _POSIX_C_SOURCE >= 200809L
45 creates a new link (also known as a hard link) to an existing file.
53 This new name may be used exactly as the old one for any operation;
54 both names refer to the same file (and so have the same permissions
55 and ownership) and it is impossible to tell which name was the
60 system call operates in exactly the same way as
62 except for the differences described here.
64 If the pathname given in
66 is relative, then it is interpreted relative to the directory
67 referred to by the file descriptor
69 (rather than relative to the current working directory of
70 the calling process, as is done by
72 for a relative pathname).
82 is interpreted relative to the current working
83 directory of the calling process (like
96 except that a relative pathname is interpreted relative
97 to the directory referred to by the file descriptor
100 The following values can be bitwise ORed in
103 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
104 .\" commit 11a7b371b64ef39fc5fb1b6f2218eef7c4d035e3
107 is an empty string, create a link to the file referenced by
109 (which may have been obtained using the
115 can refer to any type of file except a directory.
116 This will generally not work if the file has a link count of zero (files
122 The caller must have the
123 .B CAP_DAC_READ_SEARCH
124 capability in order to use this flag.
125 This flag is Linux-specific; define
127 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
128 to obtain its definition.
130 .BR AT_SYMLINK_FOLLOW " (since Linux 2.6.18)"
135 if it is a symbolic link (like
143 to be dereferenced if it is a symbolic link.
144 If procfs is mounted,
145 this can be used as an alternative to
151 linkat(AT_FDCWD, "/proc/self/fd/<fd>", newdirfd,
152 newname, AT_SYMLINK_FOLLOW);
156 Before kernel 2.6.18, the
158 argument was unused, and had to be specified as 0.
162 for an explanation of the need for
165 On success, zero is returned.
166 On error, \-1 is returned, and
168 is set to indicate the error.
172 Write access to the directory containing
174 is denied, or search permission is denied for one of the directories
175 in the path prefix of
180 .BR path_resolution (7).)
183 The user's quota of disk blocks on the filesystem has been exhausted.
190 .IR oldpath " or " newpath " points outside your accessible address space."
193 An I/O error occurred.
196 Too many symbolic links were encountered in resolving
197 .IR oldpath " or " newpath .
200 The file referred to by
202 already has the maximum number of links to it.
205 filesystem that does not employ the
207 feature, the limit on the number of hard links to a file is 65,000; on
209 the limit is 65,535 links.
212 .IR oldpath " or " newpath " was too long."
215 A directory component in
216 .IR oldpath " or " newpath
217 does not exist or is a dangling symbolic link.
220 Insufficient kernel memory was available.
223 The device containing the file has no room for the new directory
227 A component used as a directory in
228 .IR oldpath " or " newpath
229 is not, in fact, a directory.
236 The filesystem containing
237 .IR oldpath " and " newpath
238 does not support the creation of hard links.
240 .BR EPERM " (since Linux 3.6)"
241 The caller does not have permission to create a hard link to this file
242 (see the description of
243 .I /proc/sys/fs/protected_hardlinks
249 is marked immutable or append-only.
251 .BR ioctl_iflags (2).)
254 The file is on a read-only filesystem.
257 .IR oldpath " and " newpath
258 are not on the same mounted filesystem.
259 (Linux permits a filesystem to be mounted at multiple points, but
261 does not work across different mounts,
262 even if the same filesystem is mounted on both.)
264 The following additional errors can occur for
275 nor a valid file descriptor.
278 An invalid flag value was specified in
285 but the caller did not have the
286 .B CAP_DAC_READ_SEARCH
290 An attempt was made to link to the
292 file corresponding to a file descriptor created with
296 open(path, O_TMPFILE | O_EXCL, mode);
304 An attempt was made to link to a
306 file corresponding to a file that has been deleted.
310 is a relative pathname and
312 refers to a directory that has been deleted,
315 is a relative pathname and
317 refers to a directory that has been deleted.
323 is a file descriptor referring to a file other than a directory;
334 is an empty string, and
336 refers to a directory.
339 was added to Linux in kernel 2.6.16;
340 library support was added to glibc in version 2.4.
343 SVr4, 4.3BSD, POSIX.1-2001 (but see NOTES), POSIX.1-2008.
344 .\" SVr4 documents additional ENOLINK and
345 .\" EMULTIHOP error conditions; POSIX.1 does not document ELOOP.
346 .\" X/OPEN does not document EFAULT, ENOMEM or EIO.
351 Hard links, as created by
353 cannot span filesystems.
358 POSIX.1-2001 says that
362 if it is a symbolic link.
363 However, since kernel 2.0,
364 .\" more precisely: since kernel 1.3.56
365 Linux does not do so: if
367 is a symbolic link, then
369 is created as a (hard) link to the same symbolic link file
372 becomes a symbolic link to the same file that
375 Some other implementations behave in the same manner as Linux.
376 .\" For example, the default Solaris compilation environment
377 .\" behaves like Linux, and contributors to a March 2005
378 .\" thread in the Austin mailing list reported that some
379 .\" other (System V) implementations did/do the same -- MTK, Apr 05
380 POSIX.1-2008 changes the specification of
382 making it implementation-dependent whether or not
384 is dereferenced if it is a symbolic link.
385 For precise control over the treatment of symbolic links when
389 On older kernels where
391 is unavailable, the glibc wrapper function falls back to the use of
400 are relative pathnames,
401 glibc constructs pathnames based on the symbolic links in
403 that correspond to the
409 On NFS filesystems, the return code may be wrong in case the NFS server
410 performs the link creation and dies before it can say so.
413 to find out if the link got created.
421 .BR path_resolution (7),