) Differences between soft links and hard links in Linux

Source: Internet
Author: User

For a long time, Linux connections have been a little ignorant, and there are also some problems in use. These two days, I carefully checked the information on the Internet and carefully understood the hard links and soft links on Linux.

Link --------- is a method that establishes a connection between a shared file and several directory items of the users who access it.

Linux contains two types of links: hard link and soft link. Soft link is also called symbolic link ).

First, let's take a look at several terms.

Inode)
To understand the link, we must first understand the concept of an index node (inode ). In Linux, the kernel allocates an inode (index node) to each newly created file.
Each component has a unique inode number. We can simply think of inode as a pointer, which always points to the specific storage location of this file. File attributes are stored in the index node.
The cited node is copied to the inner for Fast File Access. The system locates each file by indexing nodes rather than file names.

Hard Link)
A hard link is a pointer pointing to a file index node, and the system does not re-allocate inode for it.
You can use the ln command to create a hard link.

Reference ln [Options] existingfile newfile
Ln [Options] existingfile-List Directory

Usage:
First, create a hard link for "existingfile". The file name is "newfile ".
In the "directory" directory, create a hard link with the same name for all files contained in "existingfile-list.
Commonly used [Options]:-F creates a link regardless of whether "newfile" exists or not. -N if "newfile" already exists, no link is created.

Soft link)
Soft links are also called symbolic links. This file contains the path name of another file. It can be any file or directory that can be linked to files of different file systems. It is similar to the shortcut in win.
You can use the: ln-s command to create a soft link.

Reference ln-s existingfile newfile
Ln-s existingfile-List Directory

Next we will use an instance to describe the hard link and soft link.

Now there are two files in the directory, one named AAA and the other named BBB.

Reference $ LS-il
963922-RW-r -- 1 Ocean ocean 92 aaa
963923-RW-r -- 1 Ocean ocean 95 bbb

First, create a hard link for AAA.

Reference $ ln AAA aaahard
$ LS-il
963922-RW-r -- 2 ocean 92 aaa
963922-RW-r -- 2 ocean 92 aaahard
963923-RW-r -- 1 Ocean ocean 95 bbb

Note that before creating a link, AAA displays the number of links as 1. After creating a link
1. The number of connections between AAA and aaahard is changed to 2.
2. The inode numbers of AAA and aaahard are the same, both of which are 963922.
3. The file sizes displayed by AAA and aaahard are the same, both of which are 92b.
The result of LN command is as follows: AAA and aaahard are two names of the same file. They have the same index node number and file attributes, and create a hard link of file AAA, that is
The file index node of AAA creates a new pointer on the current directory. You can delete any of them, such as RM.
AAA: Only one pointer is deleted at a time, and the number of links is reduced by one. Only the pointer pointing to the file content, that is, when the number of links is reduced to 0, the kernel will delete the file content from the disk.
Although hard link saves space, it is also a traditional way for Linux to integrate file systems, but there are some shortcomings:
1. Hard links cannot be created for directories.
2. You cannot create links between files in different file systems.

Next, we create a soft link pointing to BBB. The soft link overcomes the shortcomings of the hard link and has no restrictions on the file system. Any user can create a symbolic link pointing to the directory. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks.

Reference $ ln-s BBB bbbsoft
$ LS-il
Total usage 0
963922-RW-r -- 2 ocean 92 aaa
963922-RW-r -- 2 ocean 92 aaahard
963923-RW-r -- 1 Ocean ocean 95 bbb
963924 lrwxrwxrwx 1 Ocean ocean 3 bbbsoft-> bbb

From the results of the above link, we can see that soft links and hard links differ not only in terms of concept, but also completely in terms of implementation.
Differences:
1. A public inode number is used to hard-link the original file/linked file, indicating that they are the same file, while soft-link the original file/linked file has different inode numbers, indicates that they are two different files;
2. The soft link on the file property clearly writes the link file, but the hard link is not written, because in essence the hard link file is completely equal to the original file;
3. The number of links is different. The number of soft links does not increase;
4. The file size is different. The displayed size of the hard link file is the same as that of the original file. The size of the soft link is different from that of the original file. The BBB size is 95b, while the bbbsoft size is 3B.
In short, creating a soft link is to create a new file. When you access a linked file, the system will find that it is a linked file. It reads the linked file and finds the file to be accessed.
Of course, soft links also have the disadvantage that hard links do not exist. Because the link file contains the path information of the original file, when the original file is moved from one directory to another, access the link file, the system won't be able to find it, and the hard link won't have this defect. You can move it as you want; it also requires the system to allocate additional space for creating new index nodes and saving the original file path.

Connection count:
As we have discussed earlier, the file link counting field indicates that there are several file directories in the system with the same inode as this file, that is, there are several hard links in this file. In the preceding example, the Count values of the files AAA and aaahard are both 2.

So what is the meaning of the count field of a directory? The count of the directory also indicates how many directory items direct to this directory. However, to be detailed, you must further explain the structure of the VFS file system. For simplicity, as long as this understanding is done: (count-2) equals to the number of direct subdirectories contained in the directory (that is, only the son, not the grandson ).

For example, if the Count field of a directory/ln is 5, the/ln directory must contain three subdirectories.

Related Article

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.