*NIX has two kinds of links: hard and soft. link creates a hard link, in which two separate directory entries point to the same inode on the disk (which also increments the link count on that inode). symlink creates a soft (or symbolic) link in which the directory entry contains the name of the target file.

Hard links are separate, redundant entries in the filesystem. When you unlink one of them, the other still points to the file (although the inode's link count is decremented) so the contents on disk are not removed; once you unlink all directory entries pointing to the inode in question it will be put back on the free list and made available for reuse. You can detect a hard link by using the -i option to ls to show the inode number in listings; two directory entries hardlinked to the same file show the same inode number.

Symbolic links are more like pointers by name to real files. Removing a symlink just removes the pointer entry in the directory. Removing the target leaves the pointer dangling and trying to access through it will fail.

Update: Here's an example which may clarify hard links.

$ touch file1 $ ls -li file1 4210927 -rw-r--r-- 1 fletch wheel 0 Dec 8 11:15 file1 $ perl -e 'link( "file1" => "file2" ) or die "$!"' $ ls -li total 0 4210927 -rw-r--r-- 2 fletch wheel 0 Dec 8 11:15 file1 4210927 -rw-r--r-- 2 fletch wheel 0 Dec 8 11:15 file2 $ echo "Through file2" >> file2 $ ls -li total 16 4210927 -rw-r--r-- 2 fletch wheel 14 Dec 8 11:15 file1 4210927 -rw-r--r-- 2 fletch wheel 14 Dec 8 11:15 file2 $ cat file1 Through file2 $ rm file1 $ cat file2 Through file2 $ ls -li total 8 4210927 -rw-r--r-- 1 fletch wheel 14 Dec 8 11:15 file2

Update: Minor wording change to try and make it clear that its the link count that's a property of the inode itself that gets decremented.


In reply to Re^2: (OT) Is there any difference "link" and "copy" functions by Fletch
in thread (OT) Is there any difference "link" and "copy" functions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.