R provides two functions to remove files (and folder) from the file system:
unlink
file.remove
It's not entirely obvious what the differences are, or indeed when to use which, other than that unlink
takes some additional arguments.
Reading the source code for these functions doesn't help very much, since both simply calls a compiled C function.
What are the differences? When should you use unlink
in preference to file.remove
, or vice versa?
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.)
Delete File in R The unlink() function takes a maximum of three parameters and removes the specified file or directory. The unlink(x, recursive = TRUE) function deletes the just symbolic link if the target of such a link is a directory.
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.
The unlink command performs the unlink subroutine on a specified file. The unlink command does not issue error messages when the associated subroutine is unsuccessful; you must check the exit value to determine if the command completed normally.
My guess is simply that unlink
was present in S, since it references Becker, Chambers and Wilks (1988), whereas file.remove
(and file.copy, file.create, etc.) have been part of R since early on in order to provide a family of functions for general file manipulation.
unlink
corresponds to a very old Unix function (and certainly existed when the 1988 version of S appeared): http://en.wikipedia.org/wiki/Unlink_(Unix)
So, unlink
is there for compatibility with S, file.remove
is there as a part of R and both are maintained in order to support long existing code from S and R. Other than that simply choose the one that suits your needs (or habits) best.
I believe that the main practical difference in the behaviour of the functions is how they handle a file that is currently open, and thus which cannot be immediately deleted:
unlink()
marks the file for deletion as soon as it is not open in any other context;file.remove()
leaves the file intact, returning FALSE
, but throws a warning.Related: how to remove the file when "permisson denied" in R?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With