Welcome to the Linux Foundation Forum!

Chapter 21 - Hardlinks

In chapter 21. LINUX FILESYSTEMS AND THE VFS
I have a small problem with understanding the part with the hard links. (the bold marked part)

On the page for "Hard and Soft Links" it´s explained as:

Hard links point to an inode.​ They are made by using ln without an option. Two or more files can point to the same inode (hard link). All hard linked files have to be on the same filesystem. Changing the content of a hard linked file in one place may not change it in other places.

i create a file on my desktop and hardlink it to a file in my home folder.
when i change the file in one place the other file (hardlink) is also changed and vice-versa

lars@Lars:~/Desktop$ echo Test > Test
lars@Lars:~/Desktop$ cat Test
Test

lars@Lars:~$ ln Desktop/Test linktest
lars@Lars:~$ cat linktest
Test

lars@Lars:~$ echo test123 >> linktest
lars@Lars:~$ cat Desktop/Test
Test
test123

lars@Lars:~$ echo test123 >> Desktop/Test
lars@Lars:~$ cat linktest
Test
test123
test123

lars@Lars:~$ cat Desktop/Test
Test
test123
test123

Comments

  • I think one of the cases they may be talking about is copying files. If you have the following files and links for example:

    ln link1 file1 #hard link
    ln -s link2 file2 #soft link
    

    If you try to copy a file onto link1, file1 does not change. If you copy a file onto link2, file2 does change.

  • Thank you for your reply. i checked it again.

    I don´t get it the way as its explained in the chapter. regardless of what im doing, content is changing.
    Where is the mistake???

    Example #1

    Copy a file on the sourcefile, is changing source and hardlink content.

    lars@Lars:~/testme$ echo source > source
    
    lars@Lars:~/testme$ cat source 
    source
    
    lars@Lars:~/testme$ ln source hardlink
    
    lars@Lars:~/testme$ cat source hardlink 
    source
    source
    
    lars@Lars:~/testme$ cp test source 
    
    lars@Lars:~/testme$ cat source hardlink 
    test
    test
    

    Example #2
    Copy a file on the hardlink is changing source and hardlink content, too

    lars@Lars:~/testme$ echo source > source
    
    lars@Lars:~/testme$ ln source hardlink
    
    lars@Lars:~/testme$ cat source hardlink 
    source
    source
    
    lars@Lars:~/testme$ cp test hardlink 
    
    lars@Lars:~/testme$ cat source hardlink 
    
    test
    test
    
  • coop
    coop Posts: 915

    This is correct behaviour, this is often misunderstood. most programs should modify both instances when something is hardlinked. However, there are cases where an update is done by a different procedure; the file is copied, the update is on the copy, and the copy is then overwritten to the original location, and the link is broken. It will not happen if you just edit a file in place. For example, there is a script called ready-for.shyou may have run in preparation for this course (you can see it at https://training.linuxfoundation.org/cm/prep/ready-for.sh). If you run ./ready-for.sh --update , it does exactly what I am saying -- any hard links are broken.

    One upon a time many programs thoughtlessly broke the links when doing updates, but people tend to be more careful now and do it only by design -- we hope :)

  • Yes, i know the ready-for.sh script, used it for the ubuntu vm i use for the exercises.

    If its correct behaviour than i think i have understand the topic. At the beginning i had a problem with how its explained in the chapter.

    thank you :smile:

Categories

Upcoming Training