Sick Gaming
Understanding Linux Links: Part 1 - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Computers (https://www.sickgaming.net/forum-86.html)
+--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html)
+--- Thread: Understanding Linux Links: Part 1 (/thread-86932.html)



Understanding Linux Links: Part 1 - xSicKxBot - 10-18-2018

Understanding Linux Links: Part 1

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/understanding-linux-links-part-1.jpg" width="1500" height="874" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/10/understanding-linux-links-part-1.jpg" class="ff-og-image-inserted" /></div>
<p>Along with <code>cp</code> and <code>mv</code>, both of which we talked about at length in <a href="https://www.linux.com/blog/2018/8/linux-beginners-moving-things-around">the previous installment of this series</a>, links are another way of putting files and directories where you want them to be. The advantage is that links let you have one file or directory show up in several places at the same time.</p>
<p>As noted previously, at the physical disk level, things like files and directories don’t really exist. A filesystem conjures them up for our human convenience. But at the disk level, there is something called a <i>partition table</i>, which lives at the beginning of every partition, and then the data scattered over the rest of the disk.</p>
<p>Although there are different types of partition tables, the ones at the beginning of a partition containing your data will map where each directory and file starts and ends. The partition table acts like an index: When you load a file from your disk, your operating system looks up the entry on the table and the table says where the file starts on the disk and where it finishes. The disk header moves to the start point, reads the data until it reaches the end point and, hey presto: here’s your file.</p>
<h3>Hard Links</h3>
<p>A hard link is simply an entry in the partition table that points to an area on a disk that <b>has already been assigned to a file</b>. In other words, a hard link points to data that has already been indexed by another entry. Let’s see how this works.</p>
<p>Open a terminal, create a directory for tests and move into it:</p>
<pre>
mkdir test_dir
cd test_dir
</pre>
<p>Create a file by <a href="https://www.linux.com/blog/2018/8/linux-beginners-moving-things-around">touching</a> it:</p>
<pre>
touch test.txt
</pre>
<p>For extra excitement (?), open <i>test.txt</i> in a text editor and add some a few words into it.</p>
<p>Now make a hard link by executing:</p>
<pre>
ln test.txt hardlink_test.txt
</pre>
<p>Run <code>ls</code>, and you’ll see your directory now contains two files… Or so it would seem. As you read before, really what you are seeing is two names for the exact same file: <i>hardlink_test.txt</i> contains the same content, has not filled any more space in the disk (try with a large file to test this), and shares the same inode as <i>test.txt</i>:</p>
<pre>
$ <b>ls -li *test*</b>
16515846 -rw-r--r-- 2 paul paul 14 oct 12 09:50 hardlink_test.txt 16515846 -rw-r--r-- 2 paul paul 14 oct 12 09:50 test.txt
</pre>
<p><i>ls</i>‘s <code>-i</code> option shows the <i>inode number</i> of a file. The <i>inode</i> is the chunk of information in the partition table that contains the location of the file or directory on the disk, the last time it was modified, and other data. If two files share the same inode, they are, to all practical effects, the same file, regardless of where they are located in the directory tree.</p>
<h3>Fluffy Links</h3>
<p>Soft links, also known as <i>symlinks</i>, are different: a soft link is really an independent file, it has its own inode and its own little slot on the disk. But it only contains a snippet of data that points the operating system to another file or directory.</p>
<p>You can create a soft link using <code>ln</code> with the <code>-s</code> option:</p>
<pre>
ln -s test.txt softlink_test.txt
</pre>
<p>This will create the soft link <i>softlink_test.txt</i> to <i>test.txt</i> in the current directory.</p>
<p>By running <code>ls -li</code> again, you can see the difference between the two different kinds of links:</p>
<pre>
$ <b>ls -li</b>
total 8 16515846 -rw-r--r-- 2 paul paul 14 oct 12 09:50 hardlink_test.txt 16515855 lrwxrwxrwx 1 paul paul 8 oct 12 09:50 softlink_test.txt -&gt; test.txt 16515846 -rw-r--r-- 2 paul paul 14 oct 12 09:50 test.txt
</pre>
<p><i>hardlink_test.txt</i> and <i>test.txt</i> contain some text and take up the same space *literally*. They also share the same inode number. Meanwhile, <i>softlink_test.txt</i> occupies much less and has a different inode number, marking it as a different file altogether. Using the <i>ls</i>‘s <code>-l</code> option also shows the file or directory your soft link points to.</p>
<h3>Why Use Links?</h3>
<p>They are good for <b>applications that come with their own environment</b>. It often happens that your Linux distro does not come with the latest version of an application you need. Take the case of the fabulous <a href="https://www.blender.org/">Blender 3D</a> design software. Blender allows you to create 3D still images as well as animated films and who wouldn’t to have that on their machine? The problem is that the current version of Blender is always at least one version ahead of that found in any distribution.</p>
<p>Fortunately, <a href="https://www.blender.org/download/">Blender provides downloads</a> that run out of the box. These packages come, apart from with the program itself, a complex framework of libraries and dependencies that Blender needs to work. All these bits and piece come within their own hierarchy of directories.</p>
<p>Every time you want to run Blender, you could <code>cd</code> into the folder you downloaded it to and run:</p>
<pre>
./blender
</pre>
<p>But that is inconvenient. It would be better if you could run the <code>blender</code> command from anywhere in your file system, as well as from your desktop command launchers.</p>
<p>The way to do that is to link the <i>blender</i> executable into a <i>bin/</i> directory. On many systems, you can make the <code>blender</code> command available from anywhere in the file system by linking to it like this:</p>
<pre>
ln -s /path/to/blender_directory/blender /home/&lt;<i>username</i>&gt;/bin
</pre>
<p>Another case in which you will need links is for <b>software that needs outdated libraries</b>. If you list your <i>/usr/lib</i> directory with <code>ls -l,</code> you will see a lot of soft-linked files fly by. Take a closer look, and you will see that the links usually have similar names to the original files they are linking to. You may see <i>libblah</i> linking to <i>libblah.so.2</i>, and then, you may even notice that <i>libblah.so.2</i> links in turn to <i>libblah.so.2.1.0</i>, the original file.</p>
<p>This is because applications often require older versions of alibrary than what is installed. The problem is that, even if the more modern versions are still compatible with the older versions (and usually they are), the program will bork if it doesn’t find the version it is looking for. To solve this problem distributions often create links so that the picky application believes it has found the older version, when, in reality, it has only found a link and ends up using the more up to date version of the library.</p>
<p>Somewhat related is what happens with <b>programs you compile yourself from the source code</b>. Programs you compile yourself often end up installed under <i>/usr/local</i>: the program itself ends up in <i>/usr/local/bin</i> and it looks for the libraries it needs <i>/</i> in the <i>/usr/local/lib</i> directory. But say that your new program needs <i>libblah</i>, but <i>libblah</i> lives in <i>/usr/lib</i> and that’s where all your other programs look for it. You can link it to <i>/usr/local/lib</i> by doing:</p>
<pre>
ln -s /usr/lib/libblah /usr/local/lib
</pre>
<p>Or, if you prefer, by <code>cd</code>ing into <i>/usr/local/lib</i>…</p>
<pre>
cd /usr/local/lib
</pre>
<p>… and then linking with:</p>
<pre>
ln -s ../lib/libblah
</pre>
<p>There are dozens more cases in which linking proves useful, and you will undoubtedly discover them as you become more proficient in using Linux, but these are the most common. Next time, we’ll look at some linking quirks you need to be aware of.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>