Because unlink (and rm) does precisely that, unlink a file. It doesn't remove a file, although the effect of unlinking may cause a file to be removed.
To understand that, one first needs to know how a (Unix) filesystem works. This is a sketch of a file:
+----------+
| #i-node | +--------------+
| |------->| Content |
+----------+ | |
| |
| |
| |
| |
| |
+--------------+
It consists of two parts, an i-node with meta-information about the
file, and a data part with the actual content of the file. Except for
the pointer to the content of the file, all information stored in an
i-node is returned by the stat() call. If you look up what
stat() is returning, you will notice that it does not
return the name of the file. That's right, the name of the file is not
stored in the i-node.
Then were is the name of the file stored? For that, we need to know how directories look like. Directories look like files, they have i-nodes as well, but what is interesting for us the content part. It looks like this:
+-------------------+
| name1 | #i-node-a |
| name2 | #i-node-b |
| name3 | #i-node-c |
| name4 | #i-node-b |
+-------------------+
It is basically a table with two columns. It maps file names to i-nodes.
This is called a link. Note that the mapping isn't unique, in the
example above, name2 and name4 map to the same i-node. If
you look once again at what stat() returns, you see that the fourth
element that is returned is the number of links to the file. In the example
above, a stat() on #i-node-a or #i-node-c
would return 1 as the number of links, but doing a stat() on
#i-node-b would return 2.
What unlink is doing is that it removes one entry from the directory. A link disappears, hence the name unlink. Now, if after performing an unlink, the number of links to an i-node is 0, and the file hasn't been opened, the file will be removed by the kernel.
Hence, the question shouldn't be "why isn't unlink named rm?", but, "why isn't rm named unlink?".
In reply to Re: Why isn't there a "copy" function?
by Anonymous Monk
in thread Why isn't there a "copy" function?
by johnnywang
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |