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

  1. lars@Lars:~/Desktop$ echo Test > Test
  2. lars@Lars:~/Desktop$ cat Test
  3. Test
  4.  
  5. lars@Lars:~$ ln Desktop/Test linktest
  6. lars@Lars:~$ cat linktest
  7. Test
  8.  
  9. lars@Lars:~$ echo test123 >> linktest
  10. lars@Lars:~$ cat Desktop/Test
  11. Test
  12. test123
  13.  
  14. lars@Lars:~$ echo test123 >> Desktop/Test
  15. lars@Lars:~$ cat linktest
  16. Test
  17. test123
  18. test123
  19.  
  20. lars@Lars:~$ cat Desktop/Test
  21. Test
  22. test123
  23. test123

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

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:

    1. ln link1 file1 #hard link
    2. 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.

    1. lars@Lars:~/testme$ echo source > source
    2.  
    3. lars@Lars:~/testme$ cat source
    4. source
    5.  
    6. lars@Lars:~/testme$ ln source hardlink
    7.  
    8. lars@Lars:~/testme$ cat source hardlink
    9. source
    10. source
    11.  
    12. lars@Lars:~/testme$ cp test source
    13.  
    14. lars@Lars:~/testme$ cat source hardlink
    15. test
    16. test

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

    1. lars@Lars:~/testme$ echo source > source
    2.  
    3. lars@Lars:~/testme$ ln source hardlink
    4.  
    5. lars@Lars:~/testme$ cat source hardlink
    6. source
    7. source
    8.  
    9. lars@Lars:~/testme$ cp test hardlink
    10.  
    11. lars@Lars:~/testme$ cat source hardlink
    12.  
    13. test
    14. test
  • Posts: 916

    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:

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training