1UNLINK(2) Linux Programmer's Manual UNLINK(2)
2
3
4
6 unlink, unlinkat - delete a name and possibly the file it refers to
7
9 #include <unistd.h>
10
11 int unlink(const char *pathname);
12
13 #include <fcntl.h> /* Definition of AT_* constants */
14 #include <unistd.h>
15
16 int unlinkat(int dirfd, const char *pathname, int flags);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 unlinkat():
21 Since glibc 2.10:
22 _POSIX_C_SOURCE >= 200809L
23 Before glibc 2.10:
24 _ATFILE_SOURCE
25
27 unlink() deletes a name from the filesystem. If that name was the last
28 link to a file and no processes have the file open, the file is deleted
29 and the space it was using is made available for reuse.
30
31 If the name was the last link to a file but any processes still have
32 the file open, the file will remain in existence until the last file
33 descriptor referring to it is closed.
34
35 If the name referred to a symbolic link, the link is removed.
36
37 If the name referred to a socket, FIFO, or device, the name for it is
38 removed but processes which have the object open may continue to use
39 it.
40
41 unlinkat()
42 The unlinkat() system call operates in exactly the same way as either
43 unlink() or rmdir(2) (depending on whether or not flags includes the
44 AT_REMOVEDIR flag) except for the differences described here.
45
46 If the pathname given in pathname is relative, then it is interpreted
47 relative to the directory referred to by the file descriptor dirfd
48 (rather than relative to the current working directory of the calling
49 process, as is done by unlink() and rmdir(2) for a relative pathname).
50
51 If the pathname given in pathname is relative and dirfd is the special
52 value AT_FDCWD, then pathname is interpreted relative to the current
53 working directory of the calling process (like unlink() and rmdir(2)).
54
55 If the pathname given in pathname is absolute, then dirfd is ignored.
56
57 flags is a bit mask that can either be specified as 0, or by ORing to‐
58 gether flag values that control the operation of unlinkat(). Cur‐
59 rently, only one such flag is defined:
60
61 AT_REMOVEDIR
62 By default, unlinkat() performs the equivalent of unlink() on
63 pathname. If the AT_REMOVEDIR flag is specified, then performs
64 the equivalent of rmdir(2) on pathname.
65
66 See openat(2) for an explanation of the need for unlinkat().
67
69 On success, zero is returned. On error, -1 is returned, and errno is
70 set to indicate the error.
71
73 EACCES Write access to the directory containing pathname is not allowed
74 for the process's effective UID, or one of the directories in
75 pathname did not allow search permission. (See also path_reso‐
76 lution(7).)
77
78 EBUSY The file pathname cannot be unlinked because it is being used by
79 the system or another process; for example, it is a mount point
80 or the NFS client software created it to represent an active but
81 otherwise nameless inode ("NFS silly renamed").
82
83 EFAULT pathname points outside your accessible address space.
84
85 EIO An I/O error occurred.
86
87 EISDIR pathname refers to a directory. (This is the non-POSIX value
88 returned by Linux since 2.1.132.)
89
90 ELOOP Too many symbolic links were encountered in translating path‐
91 name.
92
93 ENAMETOOLONG
94 pathname was too long.
95
96 ENOENT A component in pathname does not exist or is a dangling symbolic
97 link, or pathname is empty.
98
99 ENOMEM Insufficient kernel memory was available.
100
101 ENOTDIR
102 A component used as a directory in pathname is not, in fact, a
103 directory.
104
105 EPERM The system does not allow unlinking of directories, or unlinking
106 of directories requires privileges that the calling process
107 doesn't have. (This is the POSIX prescribed error return; as
108 noted above, Linux returns EISDIR for this case.)
109
110 EPERM (Linux only)
111 The filesystem does not allow unlinking of files.
112
113 EPERM or EACCES
114 The directory containing pathname has the sticky bit (S_ISVTX)
115 set and the process's effective UID is neither the UID of the
116 file to be deleted nor that of the directory containing it, and
117 the process is not privileged (Linux: does not have the
118 CAP_FOWNER capability).
119
120 EPERM The file to be unlinked is marked immutable or append-only.
121 (See ioctl_iflags(2).)
122
123 EROFS pathname refers to a file on a read-only filesystem.
124
125 The same errors that occur for unlink() and rmdir(2) can also occur for
126 unlinkat(). The following additional errors can occur for unlinkat():
127
128 EBADF pathname is relative but dirfd is neither AT_FDCWD nor a valid
129 file descriptor.
130
131 EINVAL An invalid flag value was specified in flags.
132
133 EISDIR pathname refers to a directory, and AT_REMOVEDIR was not speci‐
134 fied in flags.
135
136 ENOTDIR
137 pathname is relative and dirfd is a file descriptor referring to
138 a file other than a directory.
139
141 unlinkat() was added to Linux in kernel 2.6.16; library support was
142 added to glibc in version 2.4.
143
145 unlink(): SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
146
147 unlinkat(): POSIX.1-2008.
148
150 Glibc notes
151 On older kernels where unlinkat() is unavailable, the glibc wrapper
152 function falls back to the use of unlink() or rmdir(2). When pathname
153 is a relative pathname, glibc constructs a pathname based on the sym‐
154 bolic link in /proc/self/fd that corresponds to the dirfd argument.
155
157 Infelicities in the protocol underlying NFS can cause the unexpected
158 disappearance of files which are still being used.
159
161 rm(1), unlink(1), chmod(2), link(2), mknod(2), open(2), rename(2),
162 rmdir(2), mkfifo(3), remove(3), path_resolution(7), symlink(7)
163
165 This page is part of release 5.13 of the Linux man-pages project. A
166 description of the project, information about reporting bugs, and the
167 latest version of this page, can be found at
168 https://www.kernel.org/doc/man-pages/.
169
170
171
172Linux 2021-08-27 UNLINK(2)