1 UNLINK(2) System Calls UNLINK(2)
2
3
4
5 NAME
6 unlink, unlinkat - remove directory entry
7
8 SYNOPSIS
9 #include <unistd.h>
10
11 int unlink(const char *path);
12
13
14 int unlinkat(int dirfd, const char *path, int flag);
15
16
17 DESCRIPTION
18 The unlink() function removes a link to a file. If path names a
19 symbolic link, unlink() removes the symbolic link named by path and
20 does not affect any file or directory named by the contents of the
21 symbolic link. Otherwise, unlink() removes the link named by the
22 pathname pointed to by path and decrements the link count of the file
23 referenced by the link.
24
25
26 The unlinkat() function also removes a link to a file. See fsattr(5).
27 If the flag argument is 0, the behavior of unlinkat() is the same as
28 unlink() except in the processing of its path argument. If path is
29 absolute, unlinkat() behaves the same as unlink() and the dirfd
30 argument is unused. If path is relative and dirfd has the value
31 AT_FDCWD, defined in <fcntl.h>, unlinkat() also behaves the same as
32 unlink(). Otherwise, path is resolved relative to the directory
33 referenced by the dirfd argument.
34
35
36 If the flag argument is set to the value AT_REMOVEDIR, defined in
37 <fcntl.h>, unlinkat() behaves the same as rmdir(2) except in the
38 processing of the path argument as described above.
39
40
41 When the file's link count becomes 0 and no process has the file open,
42 the space occupied by the file will be freed and the file is no longer
43 accessible. If one or more processes have the file open when the last
44 link is removed, the link is removed before unlink() or unlinkat()
45 returns, but the removal of the file contents is postponed until all
46 references to the file are closed.
47
48
49 If the path argument is a directory and the filesystem supports
50 unlink() and unlinkat() on directories the behaviour is dependent on
51 the filesystem.
52
53
54 If the path argument is a directory and the filesystem does not support
55 unlink() and unlink() on directories (for example, ZFS), the call will
56 fail with errno set to EPERM.
57
58
59 Upon successful completion, unlink() and unlinkat() will mark for
60 update the st_ctime and st_mtime fields of the parent directory. If
61 the file's link count is not 0, the st_ctime field of the file will be
62 marked for update.
63
64 RETURN VALUES
65 Upon successful completion, 0 is returned. Otherwise, -1 is returned,
66 errno is set to indicate the error, and the file is not unlinked.
67
68 ERRORS
69 The unlink() and unlinkat() functions will fail if:
70
71 EACCES
72 Search permission is denied for a component of the path
73 prefix, or write permission is denied on the directory
74 containing the link to be removed.
75
76
77 EACCES
78 The parent directory has the sticky bit set and the
79 file is not writable by the user, the user does not own
80 the parent directory, the user does not own the file,
81 and the user is not a privileged user.
82
83
84 EBUSY
85 The entry to be unlinked is the mount point for a
86 mounted file system.
87
88
89 EFAULT
90 The path argument points to an illegal address.
91
92
93 EILSEQ
94 The path argument includes non-UTF8 characters and the
95 file system accepts only file names where all
96 characters are part of the UTF-8 character codeset.
97
98
99 EINTR
100 A signal was caught during the execution of the
101 unlink() function.
102
103
104 ELOOP
105 Too many symbolic links were encountered in translating
106 path.
107
108
109 ENAMETOOLONG
110 The length of the path argument exceeds PATH_MAX, or
111 the length of a path component exceeds NAME_MAX while
112 _POSIX_NO_TRUNC is in effect.
113
114
115 ENOENT
116 The named file does not exist or is a null pathname.
117
118
119 ENOLINK
120 The path argument points to a remote machine and the
121 link to that machine is no longer active.
122
123
124 ENOTDIR
125 A component of the path prefix is not a directory or
126 the provided directory descriptor for unlinkat() is not
127 AT_FDCWD or does not reference a directory.
128
129
130 EPERM
131 The named file is a directory and {PRIV_SYS_LINKDIR} is
132 not asserted in the effective set of the calling
133 process, or the filesystem implementation does not
134 support unlink() or unlinkat() on directories.
135
136
137 EROFS
138 The directory entry to be unlinked is part of a read-
139 only file system.
140
141
142
143 The unlink() and unlinkat() functions may fail if:
144
145 ENAMETOOLONG
146 Pathname resolution of a symbolic link produced an
147 intermediate result whose length exceeds {PATH_MAX}.
148
149
150 ETXTBSY
151 The entry to be unlinked is the last directory entry to
152 a pure procedure (shared text) file that is being
153 executed.
154
155
156 USAGE
157 Applications should use rmdir(2) to remove a directory.
158
159 ATTRIBUTES
160 See attributes(5) for descriptions of the following attributes:
161
162
163
164
165 +--------------------+----------------------------------------------+
166 | ATTRIBUTE TYPE | ATTRIBUTE VALUE |
167 +--------------------+----------------------------------------------+
168 |Interface Stability | unlink() is Standard; unlinkat() is Evolving |
169 +--------------------+----------------------------------------------+
170 |MT-Level | Async-Signal-Safe |
171 +--------------------+----------------------------------------------+
172
173 SEE ALSO
174 rm(1), close(2), link(2), open(2), rmdir(2), remove(3C), attributes(5),
175 privileges(5), fsattr(5)
176
177
178
179 December 15, 2016 UNLINK(2)