The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


unlink - remove a directory entry


#include <unistd.h>

int unlink(const char *path);


The unlink() function removes a link to a file. If path names a symbolic link, unlink() removes the symbolic link named by path and does not affect any file or directory named by the contents of the symbolic link. Otherwise, unlink() removes the link named by the pathname pointed to by path and decrements the link count of the file referenced by the link.

When the file's link count becomes 0 and no process has the file open, the space occupied by the file will be freed and the file will no longer be accessible. If one or more processes have the file open when the last link is removed, the link will be removed before unlink() returns, but the removal of the file contents will be postponed until all references to the file are closed.

The path argument must not name a directory unless the process has appropriate privileges and the implementation supports using unlink() on directories.

Upon successful completion, unlink() will mark for update the st_ctime and st_mtime fields of the parent directory. Also, if the file's link count is not 0, the st_ctime field of the file will be marked for update.


Upon successful completion, 0 is returned. Otherwise, -1 is returned and errno is set to indicate the error. If -1 is returned, the named file will not be changed.


The unlink() function will fail and not unlink the file if:
Search permission is denied for a component of the path prefix, or write permission is denied on the directory containing the directory entry to be removed.
The file named by the path argument cannot be unlinked because it is being used by the system or another process and the implementation considers this an error.
Too many symbolic links were encountered in resolving path.
The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.
A component of path does not name an existing file or path is an empty string.
A component of the path prefix is not a directory.
The file named by path is a directory, and either the calling process does not have appropriate privileges, or the implementation prohibits using unlink() on directories.
The S_ISVTX flag is set on the directory containing the file referred to by the path argument and the caller is not the file owner, nor is the caller the directory owner, nor does the caller have appropriate privileges.
The directory entry to be unlinked is part of a read-only file system.

The unlink() function may fail and not unlink the file if:

The file named by path is a named STREAM.
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.
The entry to be unlinked is the last directory entry to a pure procedure (shared text) file that is being executed.




Applications should use rmdir() to remove a directory.




close(), link(), remove(), rmdir(), <unistd.h>.


Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]