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 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Haardt
29 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Modified 2005-04-04, as per suggestion by Michael Hardt for rename.2
32 .TH LINK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
34 link, linkat \- make a new name for a file
37 .B #include <unistd.h>
39 .BI "int link(const char *" oldpath ", const char *" newpath );
41 .BR "#include <fcntl.h> " "/* Definition of " AT_* " constants */"
42 .B #include <unistd.h>
44 .BI "int linkat(int " olddirfd ", const char *" oldpath ,
45 .BI " int " newdirfd ", const char *" newpath ", int " flags );
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
56 _POSIX_C_SOURCE >= 200809L
62 creates a new link (also known as a hard link) to an existing file.
70 This new name may be used exactly as the old one for any operation;
71 both names refer to the same file (and so have the same permissions
72 and ownership) and it is impossible to tell which name was the
77 system call operates in exactly the same way as
79 except for the differences described here.
81 If the pathname given in
83 is relative, then it is interpreted relative to the directory
84 referred to by the file descriptor
86 (rather than relative to the current working directory of
87 the calling process, as is done by
89 for a relative pathname).
99 is interpreted relative to the current working
100 directory of the calling process (like
109 The interpretation of
113 except that a relative pathname is interpreted relative
114 to the directory referred to by the file descriptor
117 The following values can be bitwise ORed in
120 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
121 .\" commit 11a7b371b64ef39fc5fb1b6f2218eef7c4d035e3
124 is an empty string, create a link to the file referenced by
126 (which may have been obtained using the
132 can refer to any type of file except a directory.
133 This will generally not work if the file has a link count of zero (files
139 The caller must have the
140 .BR CAP_DAC_READ_SEARCH
141 capability in order to use this flag.
142 This flag is Linux-specific; define
144 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
145 to obtain its definition.
147 .BR AT_SYMLINK_FOLLOW " (since Linux 2.6.18)"
152 if it is a symbolic link (like
160 to be dereferenced if it is a symbolic link.
161 If procfs is mounted,
162 this can be used as an alternative to
168 linkat(AT_FDCWD, "/proc/self/fd/<fd>", newdirfd,
169 newname, AT_SYMLINK_FOLLOW);
173 Before kernel 2.6.18, the
175 argument was unused, and had to be specified as 0.
179 for an explanation of the need for
182 On success, zero is returned.
183 On error, \-1 is returned, and
185 is set to indicate the error.
189 Write access to the directory containing
191 is denied, or search permission is denied for one of the directories
192 in the path prefix of
197 .BR path_resolution (7).)
200 The user's quota of disk blocks on the filesystem has been exhausted.
207 .IR oldpath " or " newpath " points outside your accessible address space."
210 An I/O error occurred.
213 Too many symbolic links were encountered in resolving
214 .IR oldpath " or " newpath .
217 The file referred to by
219 already has the maximum number of links to it.
222 filesystem that does not employ the
224 feature, the limit on the number of hard links to a file is 65,000; on
226 the limit is 65,535 links.
229 .IR oldpath " or " newpath " was too long."
232 A directory component in
233 .IR oldpath " or " newpath
234 does not exist or is a dangling symbolic link.
237 Insufficient kernel memory was available.
240 The device containing the file has no room for the new directory
244 A component used as a directory in
245 .IR oldpath " or " newpath
246 is not, in fact, a directory.
253 The filesystem containing
254 .IR oldpath " and " newpath
255 does not support the creation of hard links.
257 .BR EPERM " (since Linux 3.6)"
258 The caller does not have permission to create a hard link to this file
259 (see the description of
260 .IR /proc/sys/fs/protected_hardlinks
266 is marked immutable or append-only.
268 .BR ioctl_iflags (2).)
271 The file is on a read-only filesystem.
274 .IR oldpath " and " newpath
275 are not on the same mounted filesystem.
276 (Linux permits a filesystem to be mounted at multiple points, but
278 does not work across different mount points,
279 even if the same filesystem is mounted on both.)
281 The following additional errors can occur for
288 is not a valid file descriptor.
291 An invalid flag value was specified in
298 but the caller did not have the
299 .B CAP_DAC_READ_SEARCH
303 An attempt was made to link to the
305 file corresponding to a file descriptor created with
309 open(path, O_TMPFILE | O_EXCL, mode);
317 An attempt was made to link to a
319 file corresponding to a file that has been deleted.
323 is a relative pathname and
325 refers to a directory that has been deleted,
328 is a relative pathname and
330 refers to a directory that has been deleted.
336 is a file descriptor referring to a file other than a directory;
347 is an empty string, and
349 refers to a directory.
352 was added to Linux in kernel 2.6.16;
353 library support was added to glibc in version 2.4.
356 SVr4, 4.3BSD, POSIX.1-2001 (but see NOTES), POSIX.1-2008.
357 .\" SVr4 documents additional ENOLINK and
358 .\" EMULTIHOP error conditions; POSIX.1 does not document ELOOP.
359 .\" X/OPEN does not document EFAULT, ENOMEM or EIO.
364 Hard links, as created by
366 cannot span filesystems.
371 POSIX.1-2001 says that
375 if it is a symbolic link.
376 However, since kernel 2.0,
377 .\" more precisely: since kernel 1.3.56
378 Linux does not do so: if
380 is a symbolic link, then
382 is created as a (hard) link to the same symbolic link file
385 becomes a symbolic link to the same file that
388 Some other implementations behave in the same manner as Linux.
389 .\" For example, the default Solaris compilation environment
390 .\" behaves like Linux, and contributors to a March 2005
391 .\" thread in the Austin mailing list reported that some
392 .\" other (System V) implementations did/do the same -- MTK, Apr 05
393 POSIX.1-2008 changes the specification of
395 making it implementation-dependent whether or not
397 is dereferenced if it is a symbolic link.
398 For precise control over the treatment of symbolic links when
402 On older kernels where
404 is unavailable, the glibc wrapper function falls back to the use of
413 are relative pathnames,
414 glibc constructs pathnames based on the symbolic links in
416 that correspond to the
422 On NFS filesystems, the return code may be wrong in case the NFS server
423 performs the link creation and dies before it can say so.
426 to find out if the link got created.
434 .BR path_resolution (7),