![]() |
|
Understanding Linux Links: Part 1 - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://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 Along with Code: cpCode: mvAs 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 partition table, which lives at the beginning of every partition, and then the data scattered over the rest of the disk. 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. Hard Links A hard link is simply an entry in the partition table that points to an area on a disk that has already been assigned to a file. In other words, a hard link points to data that has already been indexed by another entry. Let’s see how this works. Open a terminal, create a directory for tests and move into it: Code: mkdir test_dir
cd test_dirCreate a file by touching it: Code: touch test.txtFor extra excitement (?), open test.txt in a text editor and add some a few words into it. Now make a hard link by executing: Code: ln test.txt hardlink_test.txtRun Code: lsCode: $ ls -li *test*
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.txtls‘s Code: -iFluffy Links Soft links, also known as symlinks, 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. You can create a soft link using Code: lnCode: -sCode: ln -s test.txt softlink_test.txtThis will create the soft link softlink_test.txt to test.txt in the current directory. By running Code: ls -liCode: $ ls -li
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 -> test.txt 16515846 -rw-r--r-- 2 paul paul 14 oct 12 09:50 test.txthardlink_test.txt and test.txt contain some text and take up the same space *literally*. They also share the same inode number. Meanwhile, softlink_test.txt occupies much less and has a different inode number, marking it as a different file altogether. Using the ls‘s Code: -lWhy Use Links? They are good for applications that come with their own environment. 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 Blender 3D 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. Fortunately, Blender provides downloads 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. Every time you want to run Blender, you could Code: cdCode: ./blenderBut that is inconvenient. It would be better if you could run the Code: blenderThe way to do that is to link the blender executable into a bin/ directory. On many systems, you can make the Code: blenderCode: ln -s /path/to/blender_directory/blender /home/<username>/binAnother case in which you will need links is for software that needs outdated libraries. If you list your /usr/lib directory with Code: ls -l,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. Somewhat related is what happens with programs you compile yourself from the source code. Programs you compile yourself often end up installed under /usr/local: the program itself ends up in /usr/local/bin and it looks for the libraries it needs / in the /usr/local/lib directory. But say that your new program needs libblah, but libblah lives in /usr/lib and that’s where all your other programs look for it. You can link it to /usr/local/lib by doing: Code: ln -s /usr/lib/libblah /usr/local/libOr, if you prefer, by Code: cdCode: cd /usr/local/lib… and then linking with: Code: ln -s ../lib/libblahThere 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. Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX. |