Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on (OT) Is there any difference "link" and "copy" functions

Replies are listed 'Best First'.
Re: (OT) Is there any difference "link" and "copy" functions
by philcrow (Priest) on Dec 08, 2006 at 15:30 UTC
    On unix a copy is a reproduction of the original. After the copy the two files lead separate lives. Link, on the other hand makes a directory entry or inode pointing to the original file. Then, there is only one disk file, but two ways to reach it.

    Phil

Re: (OT) Is there any difference "link" and "copy" functions
by swampyankee (Parson) on Dec 08, 2006 at 15:42 UTC

    Did you actually read the docs?

    On *ix, a link is a pointer to another file. Sort of like shortcuts in Windows (although better); copy (amended link) duplicates the contents of a file in a new file. Creating a link tells the directory that 'any time I refer to a file named fred, I really mean the file named Dr J Fred Mbogo.'

    As an analogy, which may be best understood by US residents. The White House and 1600 Pennsylvania Avenue, Washington, DC refer to the place, by different names.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: (OT) Is there any difference "link" and "copy" functions
by Melly (Chaplain) on Dec 08, 2006 at 16:47 UTC

    The main practical difference, afaik, is that if you edit a link, then you are editing the original as well (well, they are the same thing).

    However, if you edit a copied file, then the original is unchanged.

    Gotta go - my granny wants to learn to suck eggs...

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk
Re: (OT) Is there any difference "link" and "copy" functions
by SheridanCat (Pilgrim) on Dec 08, 2006 at 15:56 UTC
    I was set to answer the OP the same way. However, I was just testing it out, and I'm not seeing the new file as a link on my Linux box. It appears to be a separate file. There's no link attribute and when I delete the original file, the new file is intact.

    Same goes for Windows, but that was no surprise to me.

      *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.

        Thanks for all that, Fletch. I learn something new every day.