unlink(2)unlink(2)NAMEunlink - remove directory entry; delete file
The system call removes the directory entry named by the path name
pointed to by path.
When all links to a file have been removed and no process has the file
open, the space occupied by the file is freed and the file ceases to
exist. If one or more processes have the file open when the last link
is removed, only the directory entry is removed immediately so that
processes that do not already have the file open cannot access the
file. After all processes close their references to the file, if there
are no more links to the file, the space occupied by the file is then
freed and the file ceases to exist.
returns the following values:
is set to indicate the error.
If fails, is set to one of the following values:
Search permission is denied for a component of the path prefix.
Write permission is denied on the directory containing the link
to be removed.
The process does not have read/write access permission to the
The entry to be unlinked is the mount point for a mounted file
path points outside the process's allocated address space.
The reliable detection of this error is implementation
Too many symbolic links were encountered in translating the path
The length of the specified path name exceeds
bytes, or the length of a component of the path name
exceeds bytes while is in effect.
The named file does not exist (for example,
path is null or a component of path does not exist).
A component of the path prefix is not a directory.
The directory containing the file to be removed
has the sticky bit set and neither the containing direc‐
tory nor the file to be removed are owned by the effec‐
tive user ID.
The named file is a directory and
the effective user ID is not a user with appropriate
privileges. Some file systems return this error whenever
the named file is a directory, regardless of the user ID.
The directory entry to be unlinked is part of a read-only file
The entry to be unlinked is the last link to a pure procedure
(shared text) file that is being executed.
If is used on a directory that is not empty (contains files other than
and the directory is unlinked, the files become orphans, and the direc‐
tory link count is left with an inaccurate value unless they are linked
by some other directory.
If is used on a directory that is empty (contains only the files and
the directory is unlinked, but the parent directory's link count is
left with an inaccurate value.
In either of the above cases, the file system should be checked using
(see fsck(1M)). To avoid these types of problems, use instead (see
SEE ALSOrm(1), close(2), link(2), open(2), rmdir(2), remove(3C), privileges(5).