In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "what are the file links under Linux", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what are the file links under Linux" can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge.
There are two types of linked link in the Linux file system: hard link hard link and soft link soft link. Although there are significant differences between the two, they are both used to solve similar problems. They all provide access to multiple directory entries (references) of a single file, but the implementation is very different. The power of linking gives the Linux file system flexibility because everything is a file.
For example, I have found that some programs require a specific version of the library to run. When you replace the old library with the upgraded library, the program crashes, indicating that the old version library is missing. Usually, the only change in the library name is the version number. Intuitively, I just added a new library link to the program and named it after the old library name. I tried to start the program again and it worked well. A program is a game, and everyone knows that every player will try his best to keep the game going.
In fact, almost all application link libraries use a common naming convention, including the major version number in the link name, and the minor version number in the file name of the file to which the link points. For example, some of the necessary files of the program are moved from one directory to another in order to meet the Linux file system specification, and the system stores links to these files in the old directory in order to be backward compatible with programs that cannot get the new location of these files. If you make a long list of the / lib64 directory, you will find many examples of this.
Lrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.hwm->.. / usr/share/cracklib/pw_dict.hwmlrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.pwd->.. / usr/share/cracklib/pw_dict.pwdlrwxrwxrwx. 1 root root 36 Dec 8 2016 cracklib_dict.pwi->.. / usr/share/cracklib/pw_dict.pwilrwxrwxrwx. 1 root root 27 Jun 9 2016 libaccountsservice.so.0-> libaccountsservice.so.0.0.0-rwxr-xr-x. 1 root root 288456 Jun 9 2016 libaccountsservice.so.0.0.0lrwxrwxrwx 1 root root 15 May 17 11:47 libacl.so.1-> libacl.so.1.1.0-rwxr-xr-x 1 root root 36472 May 17 11:47 libacl.so.1.1.0lrwxrwxrwx. 1 root root 15 Feb 4 2016 libaio.so.1-> libaio.so.1.0.1-rwxr-xr-x. 1 root root 6224 Feb 4 2016 libaio.so.1.0.0-rwxr-xr-x. 1 root root 6224 Feb 4 2016 libaio.so.1.0.1lrwxrwxrwx. 1 root root 30 Jan 16 16:39 libakonadi-calendar.so.4-> libakonadi-calendar.so.4.14.26-rwxr-xr-x. 1 root root 816160 Jan 1616: 39 libakonadi-calendar.so.4.14.26lrwxrwxrwx. 1 root root 29 Jan 16 16:39 libakonadi-contact.so.4-> libakonadi-contact.so.4.14.26
Some links in the / lib64 directory
In the / lib64 directory inventory list shown above, the first letter l (lowercase l) of the file pattern indicates that this is a soft link (also known as a symbolic link).
Hard link
In Linux's article on the history, features, and best practices of the EXT4 file system, I discussed the fact that every file has an inode that contains information about the file, including the location of the file. Figure 2 in the above article shows a single directory entry that points to inode. Each file has at least one directory entry that points to the inode that describes the file information, and the directory entry is a hard link, so each file has at least one hard link.
As shown in figure 1 below, multiple directory entries point to the same inode. These directory entries are hard links. I have used the abbreviation for the wavy line (~) in three directory entries, which is the customary representation of the user directory, so in this case it is equivalent to / home/user. It is worth noting that the fourth directory entry is a completely different directory, / home/shared, which may be the shared file directory of the user on this computer.
Hard links are limited to a single file system. The "file system" here refers to a partition or logical volume mounted at a specific mount point, in this case / home. This is because the inode number is unique in each file system. In different file systems, such as / var or / opt, there will be the same inode number as in / home.
Because all hard links point to a single inode that contains file meta information, these attributes are part of the file, such as ownership, permissions, and the number of hard links to that inode, and these features are no different for each hard link. This is a set of properties that a file has. The only thing that can distinguish between these files is the file name contained in the inode information. Hard links to a single file / inode in the same directory must have different file names, based on the fact that there can be no duplicate file names in the same directory.
The number of hard links to the file can be checked through ls-l, and if you want to see the actual node number, use the ls-li command.
Symbolic (soft) link
The difference between a hard link and a soft link (also known as a symbolic link symlink) is that the hard link points directly to the inode that belongs to the file, while the soft link points directly to a directory entry, that is, to a hard link. Because soft links point to a file's hard link rather than the file's inode, they do not depend on the inode number, which allows them to work across different file systems, partitions, and logical volumes.
The disadvantage of a soft link is that once the hard link it points to is deleted or renamed, the soft link becomes invalid. Although the soft link is still there, the hard link you point to no longer exists. Fortunately, the ls command can highlight invalid soft links in its list in white on a red background.
Experimental project: link experiment
I think the easiest way to understand the use of links and their differences is to build a project. This project should be carried out in an empty directory as a non-superuser. I created the ~ / temp directory to do this experiment, and you can do the same. Doing so creates a secure environment for the project and provides a new empty directory for the program to run, so that only files related to the program are stored here.
Initial work
First, create a temporary directory for the tasks in the project under the directory where you want to experiment, make sure that the current working directory (PWD) is your home directory, and then type the following command.
Mkdir temp
Use this command to change the current working directory to ~ / temp.
Cd temp
At the beginning of the experiment, we need to create a file that can be linked to, and the following command completes the work and populates it with content.
Du-h > main.file.txt
Use ls-l long list naming to confirm that the file is created correctly. The running result should be similar to mine. Note that the file size is only 7 bytes, but yours may vary from 1 to 2 bytes.
[dboth@david temp] $ls-ltotal 4murr RWMurr RWMurray-1 dboth dboth 7 Jun 13 07:34 main.file.txt
In the list, the number 1 after the file pattern string represents the number of hard links that exist on the file. It should be 1 now, because we haven't established any hard links for this test file yet.
Experiment with hard links
Hard links create a new directory entry that points to the same inode. When you add a hard link to a file, you will see an increase in the number of links. Make sure the current working directory is still ~ / temp. Create a hard link to main.file.txt and view the list of files in that directory.
[dboth@david temp] $ln main.file.txt link1.file.txt [dboth@david temp] $ls-ltotal 8 RWMurray RWLY-2 dboth dboth 7 Jun 13 07:34 link1.file.txt-rw-rw-r-- 2 dboth dboth 7 Jun 13 07:34 main.file.txt
Both files in the directory have two links of the same size and the same timestamp. This is a file with one inode and two hard links (that is, the directory entries for the file). Then create a hard link to the file and list the contents of the directory list. You can create a hard link: link1.file.txt or main.file.txt.
[dboth@david temp] $ln link1.file.txt link2.file.txt; ls-ltotal 16 dboth dboth RW Jun 13 07:34 main.file.txt-3 dboth dboth 7 Jun 13 07:34 link1.file.txt-rw-rw-r-- 3 dboth dboth 7 Jun 13 07:34 link2.file.txt-rw-rw-r-- 3 dboth dboth 7 Jun 13
Note that each hard link in this directory must use a different name, because two files in the same directory cannot have the same file name. Try to create a hard link with the same name as the existing link.
[dboth@david temp] $ln main.file.txt link2.file.txtln: failed to create hard link 'link2.file.txt': File exists
Obviously not, because link2.file.txt already exists. So far we have only created a hard link in the same directory, and then a link in the parent directory of the temporary directory (your home directory).
[dboth@david temp] $ln main.file.txt.. / main.file.txt; ls-l.. / main*-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 main.file.txt
The above ls command shows that the main.file.txt file does exist in the home directory and matches the name of the file in the temp directory. Of course, they are not different files, they are two links to the same file, pointing to the directory entries of the same file. To help illustrate the next point, add a non-linked file to the temp directory.
[dboth@david temp] $touch unlinked.file; ls-ltotal 12 RW muri RW Murray-4 dboth dboth 7 Jun 13 07:34 link1.file.txt-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 link2.file.txt-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 main.file.txt-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
Use the I option of the ls command to view the hard link number of inode and the hard link number of the newly created file.
[dboth@david temp] $ls-litotal 12657024-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 link1.file.txt657024-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 link2.file.txt657024-rw-rw-r-- 4 dboth dboth 7 Jun 13 07:34 main.file.txt657863-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
Notice the number 657024 to the left of the file pattern above, which is the inode number of the same file referred to by the three hard-linked files. You can also use the I option to view the node number of the link created in the home directory, which is the same as this value. The inode number with only one link is different from the others, and the inode number you see on your system may be different from that in this article.
Then change the size of one of the hard link files.
[dboth@david temp] $df-h > link2.file.txt; ls-litotal 12657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 link1.file.txt657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 link2.file.txt657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 main.file.txt657863-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
Now all hard-link files are larger than they used to be, because multiple directory entries are linked to the same file.
The next experiment will show up on my computer because my / tmp directory is on a separate logical volume. If you have separate logical volumes or file systems on different partitions (if logical volumes are not used), make sure you can access that partition or logical volumes. If not, you can mount a U disk on your computer. If the above method suits you, you can do this experiment.
Try setting up a link to the files in the ~ / temp directory in the / tmp directory (or the location of your file system).
[dboth@david temp] $ln link2.file.txt / tmp/link3.file.txtln: failed to create hard link'/ tmp/link3.file.txt' = > 'link2.file.txt':Invalid cross-device link
Why did this mistake happen? The reason is that each individual mountable file system has its own set of inode numbers. Simply referencing a file across the entire Linux file system structure through the inode number can confuse the system because the same node number exists in each mounted file system.
Sometimes you may want to find all the hard links to an inode. You can use the ls-li command. Then use the find command to find the node numbers of all hard links.
[dboth@david temp] $find. -inum 657024./main.file.txt./link1.file.txt./link2.file.txt
Notice that the find command cannot find the four hard links of the node to which it belongs because we look in the ~ / temp directory. The find command looks for files only in the current working directory and its subdirectories. To find all the hard links, we can use the following command to specify your home directory as the starting search condition.
[dboth@david temp] $find ~-samefile main.file.txt/home/dboth/temp/main.file.txt/home/dboth/temp/link1.file.txt/home/dboth/temp/link2.file.txt/home/dboth/main.file.txt
If you are a non-superuser and do not have permissions, you may see an error message. This command also uses the-samefile option instead of specifying the node number of the file. This effect is the same as using inode numbers and is easier if you know one of the hard link names.
Experiment with soft links
As you just saw, hard links cannot be created across file system boundaries, that is, from one file system to another in a logical volume or file system. Soft links give a solution to this problem. Although they can achieve the same goal, they are very different, and it is important to know these differences.
Let's start our exploration by creating a symbolic link in the ~ / temp directory.
[dboth@david temp] $ln-s link2.file.txt link3.file.txt; ls-litotal 12657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 link1.file.txt657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 link2.file.txt658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15:21 link3.file.txt-> link2.file.txt657024-rw-rw-r-- 4 dboth dboth 1157 Jun 14 14:14 main.file.txt657863-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
The hard links with node number 657024 remain the same, and the number of hard links remains the same. The newly created symbolic link has a different inode number 658270. A soft link named link3.file.txt points to the link2.file.txt file. Use the cat command to view the contents of the link3.file.txt file. The inode information of the symbolic link begins with the letter l (lowercase l), which means that the file is actually a symbolic link.
In the example above, the size of the soft link file link3.file.txt is only 14 bytes. This is the size of the text content link3.file.txt, that is, the actual content of the catalog item. The directory entry link3.file.txt does not point to one inode; it points to another directory entry, which is helpful when establishing links across the file system. Now try to create a soft link that you tried previously in the / tmp directory.
[dboth@david temp] $ln-s / home/dboth/temp/link2.file.txt/tmp/link3.file.txt; ls-l / tmp/link*lrwxrwxrwx 1 dboth dboth 31 Jun 14 21:53 / tmp/link3.file.txt-> / home/dboth/temp/link2.file.txt
Delete Link
There are some issues to consider when you delete a hard link or a file referred to in a hard link.
First, let's delete the hard link file main.file.txt. Note that each directory entry that points to inode is a hard link.
[dboth@david temp] $rm main.file.txt; ls-litotal 8657024-rw-rw-r-- 3 dboth dboth 1157 Jun 14 14:14 link1.file.txt657024-rw-rw-r-- 3 dboth dboth 1157 Jun 14 14:14 link2.file.txt658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15:21 link3.file.txt-> link2.file.txt657863-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
Main.file.txt is the first hard link that is created when the file is created. Now delete it and still retain the original file and the data on the hard drive, as well as all the remaining hard links. To delete the original file, you must delete all its hard links.
Now delete the link2.file.txt hard link file.
[dboth@david temp] $rm link2.file.txt; ls-litotal 8657024-rw-rw-r-- 3 dboth dboth 1157 Jun 14 14:14 link1.file.txt658270 lrwxrwxrwx 1 dboth dboth 14 Jun 14 15:21 link3.file.txt-> link2.file.txt657024-rw-rw-r-- 3 dboth dboth 1157 Jun 14 14:14 main.file.txt657863-rw-rw-r-- 1 dboth dboth 0 Jun 14 08:18 unlinked.file
Pay attention to the changes in soft links. Deleting the hard link referred to in the soft link invalidates the soft link. In my system, broken links are highlighted in color, and hard links to targets flash. If you need to fix this broken soft link, you need to create a hard link with the same name as the old link in the same directory, as long as not all the hard links have been deleted. You can also recreate the link itself, leaving the link with the same name, but pointing to one of the remaining hard links. Of course, if soft links are no longer needed, you can use the rm command to remove them.
The unlink command is also useful when deleting files and links. It is very simple and has no options, just like the rm command. However, it more accurately reflects the basic process of deletion because it removes the link between the directory entry and the deleted file.
After reading this, the article "what are the file links under Linux" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.