[X-Unix] Utility "ln" makes a copy instead of hard link?

Brian L. Matthews blmatthews at gmail.com
Fri Apr 7 10:38:41 PDT 2006

>1.  Create a text file named "1.txt" containing the text "Dog".
>2.  Create a "hard link" by the command:
>         ln 1.txt
>     Expected result: Should create a "hard link" file
>     Actual result: Error message:
>         ln: ./1.txt: File exists

That's what I'd expect to happen. "ln 1.txt" is the same as "ln 1.txt 
./1.txt", but ./1.txt already exists (it's 1.txt). Try instead:

$ echo Dog >1.txt
$ mkdir 2
$ cd 2
$ ln ../1.txt

That should work and create a file in the 2 directory called 1.txt 
hardlinked to ../1.txt.

>3.  Retry to make a "hard link" by the command:
>         ln 1.txt 2.txt
>     Result: Now there are two files, 1.txt and 2.txt.
>4.  Open 1.txt and change contents to "Cat".
>5.  Save.
>6.  Open "2.txt" and read it.
>7.  Expected result: "Cat".
>8.  Actual result: "Dog"

How are you editing 1.txt? Some editors follow this procedure when 
writing a file:

write the file to a temp file
delete the original file
rename the temp file to have the same name as the original file

That breaks hard links. Instead of editing 1.txt, try:

$ echo Cat >1.txt

Then look at the 2.txt, and it should also contain Cat.

Also, if you do an ls -l, 1.txt and 2.txt should have a 2 after the 
permissions. That's the number of links to the file. Remove one of 
the two and it will drop to 1, add another hard link, it will 
increase, etc.


More information about the X-Unix mailing list