In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Links are another way to put files and directories where you want them.
In addition to cp and mv, which we discussed in detail in the previous part of this series, links are another way to put files and directories where you want them. It has the advantage of allowing you to display a file or directory in multiple locations at once.
As mentioned earlier, at the physical disk level, things like files and directories don't really exist. File systems are fictionalized for human convenience. But at the disk level, there's something called the partition table, which sits at the beginning of each partition, and then the data is scattered across the rest of the disk.
Although there are different types of partition tables, the table at the beginning of the partition contains data that maps the beginning and end of each directory and file. The partition table is like an index: when a file is loaded from disk, the operating system looks up the entries in the table, and the partition table tells where the file starts and ends on disk. Then the disk head moves to the start, reads the data until it reaches the end, and you see: here's your file.
(1) Hard links:
A hard link is simply an entry in the partition table that points to an area on disk that has been allocated to a file. In other words, a hard link points to data that is already indexed by another entry. Let's see how it works.
Open the terminal, create an experiment directory and go to:
mkdir test_dir
cd test_dir
Use touch to create a file:
touch test.txt
For more experience (?) Open test.txt in a text editor and add some words.
Now establish a hard link by executing the following command:
ln test.txt hardlink_test.txt
Run ls and you'll see that your directory now contains two files, or so it seems. As you read before, what you really see are two names for exactly the same file: hardlink_test.txt contains the same content, doesn't fill any more space on the disk (try testing with large files), and uses the same inode as test. txt:
$ ls -li test
16515846 -rw-r--r-- 2 paul paul 14 oct 1209:50 hardlink_test.txt
16515846 -rw-r--r-- 2 paul paul 14 oct 1209:50 test.txt
The-i option of ls displays the "inode value" of a file. An "inode" is a block of information in a partition table that contains the location of a file or directory on disk, the time it was last modified, and other data. If two files use the same inode, they are effectively the same file regardless of their position in the directory tree.
(2) Soft links:
A soft link, also known as symlink, is different from a hard link: a soft link is actually a separate file that has its own inode and its own small chunk of place on disk. But it contains only a small piece of data that points the operating system to another file or directory.
You can create a soft link using the-s option of ln:
ln -s test.txt softlink_test.txt
This creates a soft link in the current directory, softlink_test.txt, which points to test.txt.
Run ls -li again and you can see the difference between the two links:
$ ls -li
total 8
16515846 -rw-r--r-- 2 paul paul 14 oct 1209:50 hardlink_test.txt
16515855 lrwxrwxrwx 1 paul paul 8 oct 1209:50 softlink_test.txt -> test.txt
16515846 -rw-r--r-- 2 paul paul 14 oct 1209:50 test.txt
Hardlink_test.txt and test.txt contain some text and literally occupy the same space. They use the same inode value. Softlink_test.txt, meanwhile, takes up much less space and has a different inode value, marking it as a completely different file. Using the-l option of ls also displays the file or directory to which the soft link points.
3) Why link?
They are suitable for applications with their own environment. Your Linux distribution usually doesn't come with the latest version of the application you need. Take the excellent Blender 3D design software, which allows you to create 3D still images as well as animated movies that everyone wants on their own machine. The problem is that the current version of Blender is at least one version higher than what comes with any distribution.
Fortunately, Blender is available as an out-of-the-box download. In addition to the programs themselves, these packages contain complex libraries and dependency frameworks that Blender needs to run. All of these data and blocks are in their own directory hierarchy.
Every time you want to run Blender, you can cd it into your download folder and run:
/blender
But it's inconvenient. It would be nice if you could run the blender command from anywhere in the file system, such as the desktop command launcher.
The way to do this is to link the blender executable to the bin/directory. On many systems, you can make the blender command available by linking it anywhere in the file system, just like this.
ln -s /path/to/blender_directory/blender/home//bin
Another situation where you need links is when software needs outdated libraries. If you list your/usr/lib directory with ls -l, you'll see a lot of softlink files flash by. Take a closer look and you'll see that soft links often have names similar to the original files they link to. You might see libblah linked to libblah.so.2, and you might even notice libblah.so.2 linked to the original file libblah.so.2.1.0 accordingly.
This is because applications often require older libraries than the installed version to be installed. The problem is that even if the new version is still compatible with the old version (which it usually is), the program will have problems if it can't find the version it's looking for. To solve this problem, distributions often create links so that a fussy application believes it has found an older version, when in fact it has only found a link and ended up using a newer version of the library.
Some relate to programs you compile from source code. A program you compile yourself usually ends up installed in/usr/local, the program itself ends up in/usr/local/bin, and it looks up the libraries it needs in the/usr/local/bin directory. But suppose your new program needs libblah, but libblah is in/usr/lib, which is where all other programs will find it. You can link it to/usr/local/lib by:
ln -s /usr/lib/libblah /usr/local/lib
Or if you prefer, cd to/usr/local/lib:
cd /usr/local/lib
Then use the link:
ln -s ../ lib/libblah
There are dozens more cases where soft links prove useful, and you'll definitely find them as you become more proficient with Linux, but these are the most common. Next time, we'll look at some link weirdness you need to watch out for.
Learn more about Linux through the Linux Foundation and edX's free "Introduction to Linux" course.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.