Link files under Linux

Source: Internet
Author: User

Organized from "Bird Brother's Linux private Dishes", collation: hua ke xiao tao http://www.cnblogs.com/hust-ghtao/

There are two types of link files in Linux: Hard links and soft links. Soft link (symbolic link) is very well understood, is similar to the Windows shortcut function of the file, so that you can quickly link to the target file (or directory), the other is through the file system Inode link to generate a new file name, rather than produce a new file, This is called hard link.

1. Hard link

in the previous blog EXT2/EXT3 file System (i), we know:

    • Each file occupies an inode, and the contents of the file are pointed to by the inode record;
    • To read the file, you must go through the directory record's file name to point to the correct inode number to read.

In other words, the file name is only related to the directory, but the contents of it are related to the inode. So, is it possible that multiple filenames correspond to the same inode? This is the origin of the so-called hard link. To put it simply: Hard link simply records the Association of a new file name in a directory connected to an inode number .

For example, suppose I have a/root/crontab, which is the hard link of/etc/crontab, which means that the two filenames are connected to the same inode, and the information about the two filenames will be exactly the same:

We found that two file names were linked to inode1912701, and the properties of the files were fully consistent. And you'll notice that the second field is changed from 1 to 2, which is the "link" field that says "How many filenames are connected to this Inode". If the read data is drawn, the following is true:

1 or 2 of the directory Inode can be used to find two different file names, regardless of which file name can be referred to real that inode to read the final data. The biggest benefit of this is security, if you delete a file name, the Inode and block are still there. You can now read the correct data through another "file name". In addition, regardless of which "filename" is used to edit, the final result will be written to the inode and block, so the data can be modified.

Basic features and limitations of hard connections:

    • When you use hard link to set the link file, the disk space and the number of inode will not change, but in a directory of block more write an associated data;
    • cannot cross file system;
    • cannot be linked to a directory. If you set a hard link to a directory, the linked data needs to be linked together with all the data under the directory, which can cause considerable complexity and is not currently supported.

2.symbolic Link

Symbolic link is good to understand, is to create a new file, and this file will let the data read to the file it is connected to the filename. Since the source file is deleted, Symbilic Link's file will "not open", in fact, the source file "file name" is not found. Read for symbolic link file:

The file content read from the Inode 1th to the linked file only has a filename, which links to the correct directory according to the file name to obtain the inode of the target file, and can eventually read the correct data.

features of the soft link file:

    • The important thing about a soft link file is that it writes the "file name" of the target file.
    • This symbilic link and Windows shortcut can be equated, the file created by symbolic link is a separate new file that consumes inode and block.
    • Hard link is more secure, but more restrictive, so symbolic link is more widely used.

Link files under Linux

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.