Next: Renaming Files, Previous: Symbolic Links, Up: File System Interface
You can delete a file with unlink or remove.
Deletion actually deletes a file name. If this is the file's only name, then the file is deleted as well. If the file has other remaining names (see Hard Links), it remains accessible under those names.
The
unlinkfunction deletes the file name filename. If this is a file's sole name, the file itself is also deleted. (Actually, if any process has the file open when this happens, deletion is postponed until all processes have closed the file.)The function
unlinkis declared in the header file unistd.h.This function returns
0on successful completion, and-1on error. In addition to the usual file name errors (see File Name Errors), the followingerrnoerror conditions are defined for this function:
EACCES- Write permission is denied for the directory from which the file is to be removed, or the directory has the sticky bit set and you do not own the file.
EBUSY- This error indicates that the file is being used by the system in such a way that it can't be unlinked. For example, you might see this error if the file name specifies the root directory or a mount point for a file system.
ENOENT- The file name to be deleted doesn't exist.
EPERM- On some systems
unlinkcannot be used to delete the name of a directory, or at least can only be used this way by a privileged user. To avoid such problems, usermdirto delete directories. (In the GNU systemunlinkcan never delete the name of a directory.)EROFS- The directory containing the file name to be deleted is on a read-only file system and can't be modified.
The
rmdirfunction deletes a directory. The directory must be empty before it can be removed; in other words, it can only contain entries for . and ...In most other respects,
rmdirbehaves likeunlink. There are two additionalerrnoerror conditions defined forrmdir:
ENOTEMPTYEEXIST- The directory to be deleted is not empty.
These two error codes are synonymous; some systems use one, and some use the other. The GNU system always uses
ENOTEMPTY.The prototype for this function is declared in the header file unistd.h.