The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
A newer edition of this document exists here


rmdir - remove a directory


#include <unistd.h>

int rmdir(const char *


The rmdir() function shall remove a directory whose name is given by path. The directory shall be removed only if it is an empty directory.

If the directory is the root directory or the current working directory of any process, it is unspecified whether the function succeeds, or whether it shall fail and set errno to [EBUSY].

If path names a symbolic link, then rmdir() shall fail and set errno to [ENOTDIR].

If the path argument refers to a path whose final component is either dot or dot-dot, rmdir() shall fail.

If the directory's link count becomes 0 and no process has the directory open, the space occupied by the directory shall be freed and the directory shall no longer be accessible. If one or more processes have the directory open when the last link is removed, the dot and dot-dot entries, if present, shall be removed before rmdir() returns and no new entries may be created in the directory, but the directory shall not be removed until all references to the directory are closed.

If the directory is not an empty directory, rmdir() shall fail and set errno to [EEXIST] or [ENOTEMPTY].

Upon successful completion, the rmdir() function shall mark for update the st_ctime and st_mtime fields of the parent directory.


Upon successful completion, the function rmdir() shall return 0. Otherwise, -1 shall be returned, and errno set to indicate the error. If -1 is returned, the named directory shall not be changed.


The rmdir() function shall fail if:

Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be removed.
The directory to be removed is currently in use by the system or some process and the implementation considers this to be an error.
The path argument names a directory that is not an empty directory, or there are hard links to the directory other than dot or a single entry in dot-dot.
The path argument contains a last component that is dot.
A physical I/O error has occurred.
A loop exists in symbolic links encountered during resolution of the path argument.
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 the path argument names a nonexistent directory or points to an empty string.
A component of path is not a directory.
[XSI] [Option Start]
The S_ISVTX flag is set on the parent directory of the directory to be removed and the caller is not the owner of the directory to be removed, nor is the caller the owner of the parent directory, nor does the caller have the appropriate privileges. [Option End]
The directory entry to be removed resides on a read-only file system.

The rmdir() function may fail if:

More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
As a result of encountering a symbolic link in resolution of the path argument, the length of the substituted pathname string exceeded {PATH_MAX}.

The following sections are informative.


Removing a Directory

The following example shows how to remove a directory named /home/cnd/mod1.

#include <unistd.h>

int status; ... status = rmdir("/home/cnd/mod1");




The rmdir() and rename() functions originated in 4.2 BSD, and they used [ENOTEMPTY] for the condition when the directory to be removed does not exist or new already exists. When the 1984 /usr/group standard was published, it contained [EEXIST] instead. When these functions were adopted into System V, the 1984 /usr/group standard was used as a reference. Therefore, several existing applications and implementations support/use both forms, and no agreement could be reached on either value. All implementations are required to supply both [EEXIST] and [ENOTEMPTY] in <errno.h> with distinct values, so that applications can use both values in C-language case statements.

The meaning of deleting pathname /dot is unclear, because the name of the file (directory) in the parent directory to be removed is not clear, particularly in the presence of multiple links to a directory.

The POSIX.1-1990 standard was silent with regard to the behavior of rmdir() when there are multiple hard links to the directory being removed. The requirement to set errno to [EEXIST] or [ENOTEMPTY] clarifies the behavior in this case.

If the process' current working directory is being removed, that should be an allowed error.

Virtually all existing implementations detect [ENOTEMPTY] or the case of dot-dot. The text in Error Numbers about returning any one of the possible errors permits that behavior to continue. The [ELOOP] error may be returned if more than {SYMLOOP_MAX} symbolic links are encountered during resolution of the path argument.




Error Numbers, mkdir(), remove(), unlink(), the Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>


First released in Issue 3. Included for alignment with the POSIX.1-1988 standard.

Issue 6

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

The following changes were made to align with the IEEE P1003.1a draft standard:

End of informative text.

UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
[ Main Index | XBD | XCU | XSH | XRAT ]