LPI 101-500 – 104.6: Creating and changing symbolic and hard links
July 20, 2023

1. hardlink, softlink, ln

This lesson is about socalled links there are hard links and soft links what is a hard link? Every file in a Linux system has at least one hard link which is the pointer that points to the storage area on which the file is stored. We are only able to see and find such a file through through this so called hard link. In addition, we can also create other hard links to a file and then store them somewhere in the same file system. These files would then be linked to one another so in principle they are copies. They have the same content size, permissions and owners. When the content of one file changes, the content of the other file also changes.

If there are three hard links from a file two can be deleted and the file still exists regardless of which file came first and which is deleted. Let’s try it out. The command to link is Ln, which stands for link of course. And we have now file test one to three here, which we link to a new file now, for example, with Ln for link and then the file test one, two, three. And we want to link it to a new file. Test one, two, four. Okay, the hard link was created and the two files are linked. Now here you can see, we have now test one, two, three and test one to four. And both of the files should have the same content. Let’s check it. Test one, two, three and get test one, two, four.

And we see that the content is actually the same. I am now changing the content of test one, two, four. Okay. And now let’s have a look. Test one, two, three. And you see, I’ve only changed the test one two four file now. But since there is a hard link between these two, the test texts have been changed in both files with Lsl. We can see here in the second column how many hard links there are for the corresponding file. We see a two for both files that we have just edited. This is because a hard link is always available. And the linking of the two files has now resulted in the two. If we look at directories, we will see that each directory has at least two hard links.

Why is that? We have our two files here. And we have the one dot here and the double dot here. One dot is nothing else than the current location. And the double dot is the link that takes you one directory higher. So these two dots are also hard links. The single dot is the same place for the directory. So we always have two four hard links here. How can you recognize hard links? Unfortunately this is not as easy as with soft links as we will see it in a moment in principle you can only recognize corresponding hard links based on the idnote used which is then always the same. You can check the inodes with Lsl and then the option I for inodes and we see the corresponding I notes here on the left.

And if we look here at the two inodes, we see that they are the same. So we know that this is the heart link because normally every file has its own inode. In principle we can only tell that this is a heart link. In order to be able to search for or recognize heart link globally, you would have to build a script because there are no onboard tools with which you can display them. But I think that would lead too far for this course. The major disadvantage of heart link is that they cannot be used beyond the appropriate file system. So I can’t set a hard link from one petition to the other. That wouldn’t work.

Okay, we come to the soft links which are symbolic links comparable to the links in Windows. A soft link is just a small file that contains the path to the file to which it was linked. The path can be specified relative or absolute. If the original file is deleted, the soft link remains and thus pointing to nothing. To create a soft link we also use the Ln command, but this time with the s option for soft link. And we will now create a soft link. We have our test one, two, three and test one, two, four. We use Ln with the option S for soft link and then test one, two, three. And we want to create a soft link to test one, two, five.

In contrast to the hard link, you can recognize soft link at first glance and we are also informed directly what is linked to what. Here we see the new file test 125, which is initially shown in a different color and also shows with an error which file it is pointing to in terms of permissions. The first place here is NL, which stands for link. Incidentally, the number of hard links does not change here when we set a soft link. So let’s delete the original file. The original file is test one two three RM test one two three. We look at the result and here we see that the soft link still exists but is marked in red. This means that the link points to nothing because the file no longer exists.

Of course it can no longer be used, but we can try it out. No such file out directory. What are such soft links used for? An example from practice, assuming you are using software that itself uses Java in various configuration files of the software, we have to provide the link to the Java binary file. The Java folder structure is always the same except for the part that deals with Java version goes in the config file. We have for example, a path like this not important, not important, not important and then maybe Java 1. 8. 91 and then bin. For example, if Java is updated due to a security update and the old one is switched off, then this path is no longer correct here and the other software no longer runs accordingly.

You can no longer access this path with this configuration. So you would have to adjust all the scripts of the software manually so that it works again. To avoid such a mess, you can create a soft link to the corresponding Java folder. Then the directory would be for example XXX and then Java actual bin and exactly this part would then be stored at the config files of the software. So if there is a new Java version, it can be installed and you simply change the soft link to the new version and let it show the new directory. So although the Java version changes and in theory the Java path two, in practice everything stays the same thanks to the soft link.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!