The linux command ln

Source: Internet
Author: User
Tags inode usage

Hard links and soft links are an important concept in the Linux file system, which involves index nodes (index node, also called Inode) in the file system, and the index node object is one of the four basic concepts of the Linux virtual file system (VFS).
However, before discussing the link, it is necessary to review the Linux file system and files. The first thing we think about is file name and file content. Yes, this is really the two attributes that we are most concerned about, but in the computer's view, there are additional attributes that need to be added to achieve effective file management. These attributes can be divided into two categories from the perspective of the computer: metadata and user data, the computer is concerned about the former, because the metadata is the key to the implementation of management, and this file in the end what is stored in it does not care (accurately, the file system does not care, but does not mean that the other components do not care) User data is the focus of human attention, but humans don't care about metadata-it's a computer thing. Interestingly, the file name is not within the scope of the metadata, and the computer does not care what the file name is, which is explained later.
In this article I have carefully introduced the disk and file system, but too wordy, here to say the key:
+ When creating a filesystem, there are at least two parts: Inode table and data blocks, which store the inode, which is the file content store. And of course there are other important parts.
The file system divides all information from a file into metadata and user data in separate locations. The metadata is stored in the inode, and the user data is stored within data blocks. There are a lot of important data in the inode, but the most important thing is what block the user data is stored in. There is no need to say much about the user data for a normal file, but what exactly is a directory file? is the name of all the files in this directory and the corresponding inode, similar to a mapping table.
+ at least two basic parameters are provided when manipulating a file: path name and file name. The path name is the directory tree, and the root directory is "/". The filesystem will first find the inode of the directory where the file is located (the absolute path I guess is the recursive query from the root directory, the relative path may have a faster method), to find the directory contents of the block, from which to find the inode corresponding to the file name, You can then find the block where the file contents are located from this inode. As you can see, the filename is not a unique identifier for the file system location file. The file name is for humans to see.

And what is the link? From the face we know that the location information of the file content is stored in its inode, and the inode is unique. To find this inode, you can only go to the data block of its parent directory, where the file name and the corresponding inode mappings are recorded. And the mapping can be many-to-one, so there is a situation, there are two different file names to the same inode, of course, this mapping does not have to exist in the same mapping table, such as/etc has a file called test1, then/etc data The map of Test1 and its inode must be recorded in block, and in/user there is a file called Test2, and in the data block of/user, the same inode is corresponding to test2, in which case test1 is the hard link of test2. The reverse is also true, anyway they point to the same inode and then the same data block. As you can see, creating a hard link is actually adding an entry to the directory's data block's mapping table, and the inode for the added item already exists. The hard link only adds the contents of the data block of the directory, does not increase the total inode usage, and does not increase the total usage of the data block, the image is only added a new entry for the original file.
So what is the principle of soft connection? A soft connection is a new file in the current directory with a new inode and data block, however, the user data of the file, that is, the contents of the data block is the file name and path of the other files and other information.
You can compare the copy file, copy a file to another directory, not only to assign new files to the Inode and data block, but also to copy the contents of the source file into a new file.

The Ls-l command allows you to view the count of links for all files in a directory:

M@sys: ~/program/c_codes/tools/calculators$ ll Total dosage -Drwxrwxr-x2Mm4096  5Month4 xx: -./drwxrwxr-x3Mm4096  4Month -  One: -.. /-rw-rw-r--1Mm340  4Month -  -:Tencalculator.l-rw-rw-r--1Mm673  5Month4 xx: -calculator.y-rw-rw-r--1Mm367  4Month -  -: - Makefile

We see that for normal files, the link count is 1 (if there are no other hard links), and there is "." The link count for the directory itself represents 2. In fact, each new subdirectory in the current directory will increase the link count of the current directory by 1. But why is it 2 when a subdirectory is not available? First look at how 1 came to be: Obviously, each file has a parent directory (the root directory seems to have it too). ), then there must be a mapping of this file in the data block of the parent directory, which is the source of 1, so why is the link count for each directory file at least 2? Because in addition to the mappings in the parent directory, the current "." is represented. It is also a quote, seen from the above results, that this "." It should be recorded in the data block of the directory, so at least the number of links is 2. We also see that each subdirectory also adds the symbol "...", which represents the upper level directory, so that each additional subdirectory, the parent directory's link count will increase by 1.

You can use the LN command to create a link, ln is the most commonly used command, but not unique, and the CP command can also create a link, which you'll say later.
It is used in the format: ln source filename Link Name
Be aware of adding paths. When you do not add an option, you create a hard link, like creating a soft connection, you need to add option-S. This is the most basic use of LN, more please refer to man Ln.

Some things to note about soft and hard links:
1. Hard links can not be used in the directory;
2. Hard links can only be used for files that already exist;
3. Soft connection can be used for non-existent files;
4. The soft connection fails after the source file is moved;
5. Creating a soft connection can use a relative path, but the link file itself will be invalidated after the link is moved.

The linux command ln

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.