Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unlink vs remove in c++

What is the difference between remove and unlink functions in C++?

like image 986
SyBer Avatar asked Feb 03 '10 14:02

SyBer


People also ask

What is unlink in C?

The unlink function 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.)

What does unlink () do?

The unlink() function shall remove a link to a file. If path names a symbolic link, unlink() shall remove the symbolic link named by path and shall not affect any file or directory named by the contents of the symbolic link.

What is the difference between rm and unlink?

With a single file, rm and unlink do the same task, remove the file. As POSIX defined, rm and unlink both call to unlink() system call. In GNU rm , it calls to unlinkat() system call, which is equivalent to the unlink() or rmdir() function except in the case where path specifies a relative path.

What does it mean to unlink a file?

unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse.


1 Answers

Apart from the fact that unlink is unix-specific (as pointed out by Chris), we read in the POSIX manual:

If path does not name a directory, remove(path) is equivalent to unlink(path). If path names a directory, remove(path) is equivalent to rmdir(path).

As for the directory-passed unlink, we read:

The path argument must not name a directory unless the process has appropriate privileges and the implementation supports using unlink() on directories. (...) Applications should use rmdir() to remove a directory.

like image 101
Kornel Kisielewicz Avatar answered Sep 24 '22 10:09

Kornel Kisielewicz