Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

unlink, unlinkat - remove link

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

int unlink(const char *path);
int unlinkat(int fd, const char *path,int flag);

Description

unlink() removes the directory entry specified by the pathname pointed to by path, and
decrements the link count of the file referenced by the directory entry. 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 is no longer be accessible. If one or more processes have the file open
when the last link is removed, the space occupied by the file is not released until all references
to the file have been closed. If path is a symbolic link, the symbolic link is removed.

path should not name a directory unless the process has appropriate privileges.
Applications should use rmdir() to remove directories.

Upon successful completion, unlink() marks the st_ctime and st_mtime structure
components of the parent directory for update. If the file ́s link count is not 0, the st_ctime
structure component of the file is also marked for update.

BS2000
unlink() continues to be supported for compatibility reasons; it has the same effect as
remove(), i.e. deletes the file (see remove()). (End)

The unlinkat() function is equivalent to the unlink() or rmdir() function except when
the path parameter specifies a relative path. In this case the directory entry to be deleted is
not searched for in the current directly, but in the directory connected with the file descriptor
fd. If the file descriptor was opened without O_SEARCH, the function checks whether a
search is permitted in the connected file descriptor with the authorizations applicable for the
directory. If the file descriptor was opened with O_SEARCH, the check is not performed.

In the flag parameter, the value AT_REMOVEDIR, which is defined in the fnctl.h header,
can be transferred. In this case fd and path should be used to specify a directory and not a
normal file.

When the value AT_FDCWD is transferred to the unlinkat() function for the fd parameter,
the current directory is used.

Return val.

0

-1

if successful.

if an error occurs. errno is set to indicate the error. The file named by path
is not changed.

Errors

unlink() and unlinkat() will fail if:

 

EACCES

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.

 

EBUSY

The entry to be removed is the mount point for a mounted file system.

 

Extension

 

EFAULT

path points outside the allocated address space of the process.

 

EINTR

A signal was caught during the unlink() system call.

 

ELOOP

Too many symbolic links were encountered in resolving path. (End)

 

ENAMETOOLONG



The length of the path argument exceeds {PATH_MAX} or a component of
path is longer than {NAME_MAX}.

 

ENOENT

The named file does not exist or is an empty string. The user is not a system
administrator.

 

ENOTDIR

A component of path is not a directory.

 

EPERM

The file named by path is a directory, and the calling process does not have
appropriate privileges.

 

EROFS

The directory entry to be unlinked is part of a read-only file system.


In addition, unlinkat() fails if the following applies:

 

EACCES

The fd parameter was not opened with O_SEARCH, and the authorizations
applicable for the directory do not permit the directory to be searched.

 

EBADF

The path parameter does not specify an absolute pathname, and the fd
parameter does not have the value AT_FDCWD, nor does it contain a valid file
descriptor opened for reading or searching.

 

ENOTDIR

The path parameter does not specify an absolute pathname, and the file
descriptor fd is not connected with a directory,
or
the flag parameter has the value AT_REMOVEDIR, and path does not specify
a directory.

 

EEXIST or ENOTEMPTY



The flag parameter has the value AT_REMOVEDIR and path specifies an
unreadable directory, or hard links to the directory which differ from dot exist
or more than one entry exists in dot-dot.

 

EINVAL

The value of the flag parameter is invalid.

Notes

rmdir() is used to delete a directory.

The program environment determines whether unlink() or unlinkat() is executed for a
BS2000 or POSIX file.

See also

close(), link(), remove(), rmdir(), fcntl.h, unistd.h.