Removing a Directory

The opposite of creating a directory with mkdir(2) is the removal of a directory with rmdir(2). Its function synopsis is as follows:

#include <unistd.h>

int rmdir(const char *path);

The function returns 0 if it succeeds and -1 with the error code in errno when it fails. The directory name given by path must be empty in order to succeed. If the directory is not empty, the error ENOTEMPTY is returned.

Note

ENOTEMPTY—Directory not empty This error indicates that the directory pathname given to rmdir(2) contains one or more files or subdirectories (or any other file system object). Files must all be released with the unlink(2) function prior to releasing the directory containing them.


Warning

HPUX documents that rmdir(2) will not remove the root directory. While it is hard to imagine a situation where this functionality would be desirable, it may be an important consideration in a specialized application.


Note

Some platforms may not permit you to remove the current working directory for the current process (for example, HPUX and SGI IRIX prevent this). See the Note about EINVAL, later in this section.

However, most UNIX platforms will permit the current directory to be deleted by a different process (HPUX, for example).


The rmdir(2) function is capable of returning a number of different errors. Two that will be introduced here are EBUSY and EINVAL.

Note

EBUSY—Device busy In the context of rmdir(2), this error code indicates that the directory is a mount point and cannot be deleted until the file system is unmounted.

EINVAL—Invalid argument This error return from rmdir(2) indicates that the directory to be removed is the current directory.


The following shows how the empty directory /tmp/my_dir is deleted:

int z;

z = rmdir("/tmp/my_dir");
if ( z == -1 )
    /* Report error */

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.129.19