[Previous]
[Contents]
[Next]

rmdir()

delete an empty directory

Synopsis:

#include <sys/types.h>
#include <unistd.h>
int rmdir( const char *path );

Description:

The rmdir() function removes (deletes) the specified directory. The directory must not contain any files or directories. The path can be either relative to the current working directory, or it can be an absolute path name.


Note: You can't use rmdir() to delete the root directory.

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

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

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EACCES
Search permission is denied for a component of path, or write permission is denied on the parent directory of the directory to be removed.
EBUSY
The directory named by the path argument can't be removed because it's being used by another process, and the implementation considers this to be an error.
EEXIST
The path argument names a directory that isn't empty.
ENAMETOOLONG
The argument path exceeds PATH_MAX in length, or a pathname component is longer than NAME_MAX.
ENOENT
The specified path doesn't exist, or path is an empty string.
ENOTDIR
A component of path isn't a directory.
ENOTEMPTY
The path argument names a directory that isn't empty.
EROFS
The directory entry to be removed resides on a read-only file system.

Examples:

To remove the directory called /home/terry

#include <sys/types.h>
#include <sys/stat.h>

int main( void )
  {
    rmdir( "/home/terry" );
    return (EXIT_SUCCESS);
  }

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

chdir(), chmod(), errno, getcwd(), mkdir(), stat(), umask()


[Previous]
[Contents]
[Next]