unlink (Unix)

{{About|the Unix system call|the mathematical concept|unlink}}

{{lowercase|title=unlink}}

{{Infobox software

| name = unlink

| logo =

| screenshot =

| screenshot size =

| caption =

| author =

| developer =

| released =

| latest release version =

| latest release date =

| operating system = Unix and Unix-like

| platform = Cross-platform

| genre = Command

| license =

| website =

}}

In Unix-like operating systems, unlink is a system call and a command line utility to delete files. The program directly interfaces the system call, which removes the file name and (but not on GNU systems) directories like rm and rmdir.{{cite web|url=https://www.gnu.org/software/coreutils/manual/html_node/unlink-invocation.html|title=GNU Coreutils: unlink invocation|website=www.gnu.org}} If the file name was the last hard link to the file, the file itself is deleted as soon as no program has it open.{{cite web|url=https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html|title=unlink|website=pubs.opengroup.org}}

Unlike the rm utility, the unlink utility only accepts one argument, which can be desirable to guard against accidental multi-deletions.{{web archive |url=https://archive.today/20241218171826/https://www.baeldung.com/linux/rm-vs-unlink |title=Differences Between rm and unlink Commands | Baeldung on Linux |date=2024-12-18 |acccess-date=2025-03-10 }}

It also appears in the PHP, Node.js, R, Perl and Python standard libraries in the form of the unlink() built-in function. Like the Unix utility, it is also used to delete files.{{cite web|url=https://php.net/unlink|title=PHP: unlink - Manual|website=php.net}}{{cite web|url=https://perldoc.perl.org/functions/unlink.html|title=unlink - perldoc.perl.org|website=perldoc.perl.org}}{{cite web|url=https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback|title=File System - Node.js v13.0.1 Documentation|website=nodejs.org}}{{cite web|url=https://docs.python.org/3/library/os.html#os.unlink|title=os — Miscellaneous operating system interfaces — Python 3.8.0 documentation|website=python.org}}

Examples

To delete a file named foo, one could type:

% unlink foo

In PHP, one could use the following function to do the same:

unlink("foo");

The Perl syntax is identical to the PHP syntax, save for the parentheses:

unlink "foo";

In Node.js it is almost the same as the others:

fs.unlink("foo", callback);

In R (with the S language compatibility):

unlink("foo")

  1. Comment: using the inside argument 'recursive = TRUE', directories can be deleted

Similarly in Python:

os.unlink("foo")

See also

References

{{Reflist}}

{{Unix commands}}

{{Core Utilities commands}}

Category:Unix SUS2008 utilities

Category:System calls