Understanding the Linux link

Someone said to me not know much about Linux links, today I come to the popularity of what is Linux link:

 

Links can be placed in the files and directories you another way to put them in a position of hope.

 

In addition to these two cp and mv we discussed in detail in the first part of this series, links to files and directories you can put another way to put them in a position of hope. The advantage is that allows you to simultaneously display a file or directory in multiple locations.

 

As mentioned earlier, this level on a physical disk, file and directory or something like that does not really exist. File system is to facilitate human use, the fictional them out. But at the disk level, there is a thing called the partition table partition table, which is located at the beginning of each partition, and then the data is dispersed in the rest of the disk.

 

Although there are different types of partition table, but the data in the table included at the beginning of the partition map the location of the start and end of each directory and file. Partition table is like an index: When a file is loaded from disk, the operating system looks for entries in the table, partition table will show the start and end location of the file on disk. Then the disk head moves to the starting point, read data until it reaches the end, you see: This is your file.

 

Hard links

 

Hard link is just an entry in the partition table that points to an area on the disk, indicating that the region has been allocated to the file. In other words, hard links to other data entries have been indexed. Let's see how it works.

 

Open a terminal, create a test directory and enter:

 

mkdir test_dir

cd test_dir

Use touch to create a file:

 

touch test.txt

To get more experience (?), Open test.txt in a text editor and add some words.

 

Now to create hard links by executing the following command:

 

ln test.txt hardlink_test.txt

Run ls, you will see your directory now contains two files, or so it seemed. As you read before, you really see is the name of two identical files: hardlink_test.txt contain the same content, there is no more space to fill disk (you can try using large file to test), test.txt and with the same inode:

 

$ 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.txt

ls -i option displays "inode value" of a file. "Inode" in the partition table information block, which contains the location of the file or directory on the disk, last modified time, and other data. If two files with the same inode, so regardless of their location in the directory tree, they are actually the same file.

 

Soft links

 

Soft link, also called a symbolic link symlink, it is different with a hard link: soft link is actually a separate file, it has its own inode and its own small area on the disk. But it contains only small amount of data, the operating system that points to another file or directory.

 

You can use ln -s option to create a soft link:

 

ln -s test.txt softlink_test.txt

This will create a soft link softlink_test.txt in the current directory, which points to test.txt.

 

Perform ls -li again, you can see the difference of two kinds of links at:

 

$ 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.txt

hardlink_test.txt and test.txt contains text literal and occupy the same space. They use the same inode number. At the same time, softlink_test.txt occupy much less, and have different inode number, mark it as a completely different file. Use ls -l option will display a file or directory soft link points.

 

Why link?

 

They are suitable for applications with their own environment. Your Linux distribution does not usually included with the latest version of your application's needs. With excellent Blender 3D design software, for example, Blender allows you to create 3D still images and animated movies, everyone wants to have it on their machines. The problem is that the current version of Blender is at least a version that comes with any release ratio.

 

Fortunately, Blender can provide download-to-use out of the box. In addition to the program itself, the package also includes a complex framework of libraries and dependencies required to run Blender. All these data blocks and in their own directory hierarchy.

 

Every time you want to run Blender, you can download it to your cd folder and run:

 

./blender

But it is very convenient. It would be better if you can, such as a desktop command launcher run blender command from anywhere file system.

 

Way to do this is to link blender executable file to bin / directory. On many systems, you can link it to the file system anywhere in the blender to make available commands, like this.

 

ln -s /path/to/blender_directory/blender /home/<username>/bin

Another case is that you need to link the software needs outdated libraries. If you list your / usr / lib directory with ls -l, you will see a lot of soft link file flashed. A closer look, you'll see a link to their usual soft links to the original files with similar names. You might see libblah link to libblah.so.2, you may even notice libblah.so.2 the link to the original file libblah.so.2.1.0.

 

This is because applications typically need to be installed older than the version already installed library. The problem is that even if the new version is still compatible with older versions (usually), if it can not find the program is looking for the version of the program will have problems. To solve this problem, release often create links to critical applications it believes it has found an older version, in fact, it only found a link and end up using an updated version of the library.

 

We are with you compile from source code related. Compile your own programs usually end installed in / usr / local, the program itself finally in / usr / local / bin, which it needs to find a library in / usr / local / bin directory. But suppose your new program needs libblah, but libblah in / usr / lib, which is that all other programs will find its place. You can by doing the following link it to / usr / local / lib:

 

ln -s /usr/lib/libblah /usr/local/lib

Or if you prefer, you can cd to / usr / local / lib:

 

cd /usr/local/lib

Then use the link:

 

ln -s ../lib/libblah

There are dozens of cases have proved soft link is useful when you are using Linux more skilled, you will certainly find them, but these are the most common. Next time, we will look at some of the links you need to look weird.

 

For more information about Linux through the Linux Foundation and edX free "Introduction to Linux" course.

 

Well, this is Linux links. More Welcome www.dianyu.net.

Guess you like

Origin www.cnblogs.com/dianyu/p/11857977.html