The difference between a Linux soft link and hard links

Source: Internet
Author: User

The difference between a Linux soft connection and a hard link 2011-06-27Zale's picture ... read 3345 transfer Go to my library.Share:
4 points Different:
(1) Soft connection can cross file system, hard connection is not possible. The practice is to use shared files to connect aa.txt text documents under Windows to Linux under the/root directory BB,CC. Ln-s Aa.txt/root/bb Connection succeeded. ln aa.txt/root/bb failed.
(2) Questions about the I node. Hard connections No matter how many, all point to the same I node, will increase the number of node connections, as long as the junction number of connections is not 0, the file exists, regardless of whether you delete the source file or the connected file. As long as there is one exists, the file exists (in fact, it does not divide the source file connection files, because they point to the same I node). When you modify the source file or any one of the connection files, the other files will be changed synchronously. Soft links do not use the I node number directly as a file pointer, but instead use the file path masterpiece as a pointer. Therefore, deleting a connection file has no effect on the source file, but deleting the source file will cause the connection file to be unable to find the file to point to. A soft link has its own inode, and there is a small space on the disk where the path name is stored. (What does this paragraph mean?) )
(3) A soft connection can be connected to a file name that does not exist.
(4) The soft connection can be connected to the directory.

Note: I node: it is a data structure used internally by UNIX to describe the characteristics of a file. We usually call the I node the file index node (information node). The I node contains important information about most of the file, including the address of the file data block on disk. Each I node has its own flag number, which we call the file sequence number.  I node contains information 1. file type 2. File is the main relationship 3. Access to files 4. The time of the file is truncated. All along, the link to Linux is a bit mengmengdongdong, the use of some problems, these two days carefully on the Internet to check the information, carefully understand the Linux hard links and soft links.

A link---------is a way to establish a connection between a shared file and a number of directory entries for the user who accesses it.

There are two types of links in Linux: Hard links and soft links (Soft link), and soft links called Symbolic links (symbolic link).

Let's take a look at some nouns first.

index Node (inode)
To understand the link, we first need to understand a concept called an index node (inode). In a Linux system, the kernel allocates an Inode (index node) for each newly created file, each with a unique inode number, and we can simply interpret the inode as a pointer that will always point to the specific storage location of this file. The file attributes are stored in the index node, and the index nodes are copied to the file when they are accessed, enabling quick access to the file. The system locates each file through an index node rather than a file name.

Hard Link
Hard links plainly is a pointer to a file index node, and the system does not redistribute the inode for it.
You can use the: ln command to create a hard link.
Refer to ln [options] Existingfile NewFile
ln [options] Existingfile-list Directory
Usage:
The first creates a hard link for "existingfile", and the file name is "NewFile".
The second in the directory directory, create a hard link with the same name for all files contained in "Existingfile-list".
Common optional [options]:-f the link is created regardless of whether the newfile exists or not. -N If "NewFile" already exists, no link is created.

Soft links (Soft link)
A soft link is also called a symbolic link, which contains the path name of another file. can be any file or directory that can link files from different file systems. And win under the same shortcut.
You can use the: ln-s command to create a soft link.
Reference Ln-s Existingfile NewFile
Ln-s existingfile-list Directory

Below we use an example to illustrate hard links and soft links.

Now there are two files in the directory, one is called AAA, and the other is called BBB.
Reference $ ls-il
963922-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 AAA
963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB

First make a hard link to AAA.
Reference $ ln AAA Aaahard
$ls-il
963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 AAA
963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 Aaahard
963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB
Here we note that before creating a link, the number of links displayed by AAA is 1, after the link is created
The number of links between 1.AAA and Aaahard becomes 2.
The inode numbers for the 2.AAA and Aaahard are the same, all 963922.
The 3.AAA and Aaahard display the same file size, which is 92B.
The results of the operation of the LN command are visible: AAA and Aaahard are two names of the same file, they have the same index node number and file attributes, and a hard link to file AAA is to create a new pointer on the current directory for the AAA File Index node. You can delete any one of them, such as RM AAA, and delete only one pointer at a time, minus one at a time, and the kernel will delete the contents of the file from the disk only if all pointers to the contents of the file, that is, the number of links is reduced to 0 o'clock.
Although hard links save space and are the traditional way for Linux systems to consolidate file systems, there are some shortcomings:
1. You are not allowed to create hard links to directories.
2. It is not possible to create links between files in different file systems.

Then we do a soft link to the BBB, soft link overcomes the lack of hard links, without any file system restrictions, any user can create a symbolic link to the directory. It is now more widely used, it has greater flexibility, and can even link files across different machines and networks.
Reference $ ln-s BBB Bbbsoft
$ ls-il
Total dosage 0
963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 AAA
963922-rw-r--r--2 Ocean Ocean 2007-05-18 15:46 Aaahard
963923-rw-r--r--1 Ocean Ocean 2007-05-18 15:46 BBB
963924lrwxrwxrwx 1 Ocean Ocean 3 2007-05-18 15:47 bbbsoft, BBB
From the results of the link above can be seen in soft links and hard links, the difference is not only conceptually, in the implementation is also completely different.
Difference:
1. Hard link Original file/link file common an inode number, stating that they are the same file,The soft link original file/link file has a different inode number, indicating that they are two different files;
2. On the file properties, the soft link explicitly writes out the link file, and the hard link is not written.Because in essence the hard link file and the original document are completely equal relations;
3. The number of links is not the same, the number of links to soft links will not increase;
4. File size is not the same, hard-link file display size is the same as the original file. The size of the soft link shown here is different from the original file, the BBB size is 95B, and the Bbbsoft is 3 B.
In short, building a soft link is aNew file. when accessing a linked file, the system will find that he is a link file that reads the link file to find the file that is actually being accessed.
Of course, soft links also have the disadvantage of hard links, because the link file contains the path information of the original file, so when the original file from one directory to another directory, and then access the linked file, the system will not be found, and the hard link does not have this flaw, you want to move how to move, and it wants to system Allocate additional space to create a new index node and save the path to the original file.

Connection Count Count:
Earlier we introduced, the link count field of the file indicates that there are several file directory entries in the system inode and the same file, that is, there are several hard links in this document. As in the above example, the count value of the AAA and Aaahard files is 2.

So what does the Count field mean for a directory? The count of the catalog also indicates how many directory entries are pointing to this directory, but it is important to explain further the structure of the VFS file system, as long as it is simple to understand:(count-2) equals the number of direct subdirectories contained in this directory (that is, only the son, not including grandchildren).

For example: If the Count field for a directory/ln is 5, then the/LN directory must contain 3 subdirectories.

The difference between a Linux soft link and hard links

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.