[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ale] link

Hi Terry,

On 4/11/07, Terry Bailey <terry at bitlinx.com> wrote:
> Hi,
> If you do a
> ln file1.abc file2.xyz
> is there a way to erase one and they both go away?

No.  The hard link means that both directory entries point to the same
data, and you must remove both of them to unlink the underlying
storage.  However it is possible to empty the contents of the file,
which will be reflected in both links.

By contrast if you use a symbolic link (ln -s) then removing the first
directory entry would effectively remove the second one, although the
symlink itself would remain (only it would point at something that
doesn't exist).

> Also, it shows that they both are the same size. Is it the case that
> there is only one instance of the data.

Yes, there is only one instance of the data.  If you use the command
`ls -li' you will see that both files have the same inode.  The inode
is the thing that actually represents the file contents.  The file
names are really just directory entries that point to the inode (in
this case, to the same inode).  That's part of the underlying
structure of a *NIX-style filesystem: a directory is really just a
list of name-inode pairs, and the inode itself contains the file
ownership, permissions, size (and other attributes) as well as
pointers to the data blocks that are the file contents.  Notice that
this means if you change the permissions or owner on one filename, it
also changes for the other one.

> If this is not the case, then you might as well do cp file1.abc file2.xyz
> In addition, which is quicker
> ln file1.abc file2.xyz    or      cp file1.abc file2.xyz

Hard linking is decidedly quicker than a copy, because you are only
creating a directory entry, not actually copying any data.  (It does
increment the link count in the inode, but that's pretty cheap.)  A
copy involves opening the original file, allocating a new inode,
setting properties in that, allocating disk blocks for the data,
copying data from the original file, and *then* creating a directory

Bj?rn Gustafsson