1LINK(2) Linux Programmer's Manual LINK(2)
2
3
4
6 link - make a new name for a file
7
9 #include <unistd.h>
10
11 int link(const char *oldpath, const char *newpath);
12
14 link() creates a new link (also known as a hard link) to an existing
15 file.
16
17 If newpath exists it will not be overwritten.
18
19 This new name may be used exactly as the old one for any operation;
20 both names refer to the same file (and so have the same permissions and
21 ownership) and it is impossible to tell which name was the "original".
22
24 On success, zero is returned. On error, -1 is returned, and errno is
25 set appropriately.
26
28 EACCES Write access to the directory containing newpath is denied, or
29 search permission is denied for one of the directories in the
30 path prefix of oldpath or newpath. (See also path_resolu‐
31 tion(7).)
32
33 EDQUOT The user's quota of disk blocks on the file system has been
34 exhausted.
35
36 EEXIST newpath already exists.
37
38 EFAULT oldpath or newpath points outside your accessible address space.
39
40 EIO An I/O error occurred.
41
42 ELOOP Too many symbolic links were encountered in resolving oldpath or
43 newpath.
44
45 EMLINK The file referred to by oldpath already has the maximum number
46 of links to it.
47
48 ENAMETOOLONG
49 oldpath or newpath was too long.
50
51 ENOENT A directory component in oldpath or newpath does not exist or is
52 a dangling symbolic link.
53
54 ENOMEM Insufficient kernel memory was available.
55
56 ENOSPC The device containing the file has no room for the new directory
57 entry.
58
59 ENOTDIR
60 A component used as a directory in oldpath or newpath is not, in
61 fact, a directory.
62
63 EPERM oldpath is a directory.
64
65 EPERM The file system containing oldpath and newpath does not support
66 the creation of hard links.
67
68 EPERM (since Linux 3.6)
69 The caller does not have permission to create a hard link to
70 this file (see the description of /proc/sys/fs/pro‐
71 tected_hardlink in proc(5)).
72
73 EROFS The file is on a read-only file system.
74
75 EXDEV oldpath and newpath are not on the same mounted file system.
76 (Linux permits a file system to be mounted at multiple points,
77 but link() does not work across different mount points, even if
78 the same file system is mounted on both.)
79
81 SVr4, 4.3BSD, POSIX.1-2001 (but see NOTES).
82
84 Hard links, as created by link(), cannot span file systems. Use sym‐
85 link(2) if this is required.
86
87 POSIX.1-2001 says that link() should dereference oldpath if it is a
88 symbolic link. However, since kernel 2.0, Linux does not do so: if
89 oldpath is a symbolic link, then newpath is created as a (hard) link to
90 the same symbolic link file (i.e., newpath becomes a symbolic link to
91 the same file that oldpath refers to). Some other implementations
92 behave in the same manner as Linux. POSIX.1-2008 changes the specifi‐
93 cation of link(), making it implementation-dependent whether or not
94 oldpath is dereferenced if it is a symbolic link. For precise control
95 over the treatment of symbolic links when creating a link, see
96 linkat(2).
97
99 On NFS file systems, the return code may be wrong in case the NFS
100 server performs the link creation and dies before it can say so. Use
101 stat(2) to find out if the link got created.
102
104 ln(1), linkat(2), open(2), rename(2), stat(2), symlink(2), unlink(2),
105 path_resolution(7), symlink(7)
106
108 This page is part of release 3.53 of the Linux man-pages project. A
109 description of the project, and information about reporting bugs, can
110 be found at http://www.kernel.org/doc/man-pages/.
111
112
113
114Linux 2013-01-27 LINK(2)