Removing Files

Removing files is pretty easy. Use the unlink() system call:

int unlink (const char *path);

unlink() returns zero on successful completion or -1 if there was an error, with errno set to the error that actually happened. There can be multiple references to a file via hardlinks. unlink() removes a reference from a particular dictionary, but the file’s contents are only removed when all references to it have been eliminated.

There is one subtlety when unlinking files that are currently open. The directory entry for the file is removed immediately. If you do an ls on the directory, you won’t see the file. But the file’s contents still exist on the disk, still consume space, and are still available to programs that have the file open. ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.