Summary of Linux hard links and soft links

Source: Internet
Author: User

Describe:

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 soft links and hard links (soft link) in Linux, where soft links are also called Symbolic links (symbolic link).

Index nodes : The management of files in Linux systems is essentially managed by their index nodes. From a system perspective, the file's index node (inode) is a unique identifier for the file that contains all the information that the filesystem needs to process the file. There are actually two types of index nodes:

1, Kernel index node (in-core indoe): In memory, each open file in the system corresponds to a kernel index node

2, Disk index node (on-disk inode): Each file in the file system has a disk index node, which is stored on disk, and the specific information it holds is related to the type of file system.

Note : The relationship between the two index nodes is that when a process opens a file, the information in the file's disk index node is loaded into memory and a kernel index node is established. When the kernel index node is modified, the system is responsible for synchronizing it to disk. The disk index node does not have the exact same information as the corresponding kernel index node. The kernel index node records some of the more general information about the file, ignoring some of the information that is relevant to the specific file system type.


Description
Hard link : is to have a file corresponding to one or more file names, or the file name and the filesystem used by the node number linked together, these files can be in the same directory or different directories. A file name corresponds to multiple file names, called the number of links to the file.

Syntax Format:

LN [options] Existingfile NewFile
ln [options] Existingfile-list Directory


usage :
        The first type is "Existingfile" Create a hard link with the file name         second type in "directory" " existingfile-list "
        Common optional [options]

- F no matter "NewFile" exist or not, both create links. - n if "NewFile" already exists, no link is created.

Features:

1, directory does not support hard links

2, hard links cannot cross file systems

3, creating a hard link increases the Inode's reference count


Soft Links: Also known as symbolic links, is actually a special file, this file contains another corpus that the person a path name. This path name points to any file located on any file system and can even point to a nonexistent file. The system automatically converts the operation of the symbolic link to the operation of the source file. Its use is similar to the use of hard links,

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.
Syntax Format

Ln-s Existingfile NewFile
Ln-s existingfile-list Directory

features :

1, the symbolic link and the file are two separate files, each with its own inode, the link to create a symbol for the original file does not increase the reference count

2, support to create symbolic links to the directory, can cross the file system

3, delete the symbolic link file does not affect the original file, but delete the original file, the symbol specified by the path does not exist, this will become invalid link


Example:

Now there are two files in the directory, one named A, one named B

[[email protected] tmp]# Ls-il Total usage 161048592-rw-r--r--1 root root 105 July 09:04 a1048593-rw-r--r--1 root root 921 July 09:04 B

First, make a hard link to a file:

[[Email protected] tmp]# ln A aaa[[email protected] tmp]# ls-il total usage 201048592-rw-r--r--2 root root 105 July 09:04 A10 48592-rw-r--r--2 root root 105 July 09:04 aaa1048593-rw-r--r--1 root root 921 July 09:04 B

Here we notice that before the link is created, the number of links shown in A is 1, and the following changes occur after the link is created:

1, the number of links for a and AAA is changed to 2.

2, A and AAA have the same inode number, all 1048592.

3,a and AAA display the same file size, both 105B

The result of the operation of the LN command is visible: A and AAA are two names of the same file, they have the same index node number and file attributes, establish a hard link to file A, that is, a file index node in the current directory to create a new pointer. Can delete any one of them, such as RM A, each time only a pointer is deleted, the number of links minus one, only the pointer to the contents of the file, that is, the link number is reduced to 0 o'clock, the kernel will not delete the contents of the file on the disk.

[[email protected] tmp]# \rm a[[email protected] tmp]# ls-il Total usage 161048592-rw-r--r--1 root root 105 July 09:04 Aaa
[[email protected] tmp]# \rm aaa[[email protected] tmp]# ls-il Total usage 121048593-rw-r--r--1 root root 921 July 09:04 B

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.

[[Email protected] tmp]# ln file1/rootln: "FILE1": Hard links are not allowed to be directed to the directory

2. It is not possible to create links between files in different file systems.


then we make a soft link to B, soft link overcomes the lack of hard links, no file system restrictions, any user can create a symbolic link to the directory. It is now more widely used, with greater flexibility and even the ability to link files across different machines and networks.

[[email protected] tmp]# ln -s b bbb[[ Email protected] tmp]# ls -il Total Dosage  201048592 -rw-r--r--  2 root  root 105 7 Month   29 09:42 a1048592 -rw-r--r--  2 root  root 105 7 Month   29 09:42 aaa1048593 -rw-r--r--  1 root  root 921 7 Month   29 09:42 b1048594 lrwxrwxrwx  1 root  root   1 7 month   29 09:43 bbb -> b 

The difference between a soft link and a hard link can be seen from the results of the link above:

1. Hard link Original file/link file common an inode number, stating that they are the same file, and the soft link original file/link file has a different inode number, indicating that they are two different files;
2. The soft link in the file attribute explicitly writes out is the link file, and the hard link is not written out, because in essence the hard link file and the original file is completely equal relationship;
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 B size is 921, and the BBB is 1. The soft link file size refers to the number of bytes of the file path string that it specifies.
5, delete the symbolic link file does not affect the original file, but delete the original file, the symbol specified by the path does not exist, this will become invalid link, this time will be an error, the file changed to red has been flashing, if the current directory to create a file named B, it will not blink, because the path to find the file point

[[email protected] tmp]# \rm bbb[[email protected] tmp]# ls-il total usage 201048592-rw-r--r--2 root root 105 July 09:42 A104 8592-rw-r--r--2 root root 105 July 09:42 aaa1048593-rw-r--r--1 root root 921 July 09:42 B

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/21/wKiom1eavDvSo1UuAAAlSdWkXag187.png "title=" QQ picture 20160729101544.png "alt=" Wkiom1eavdvso1uuaaalsdwkxag187.png "/>

6, allows to create symbolic links to the directory

[Email protected] ~]# ls-s/tmp/file file

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/21/wKioL1eawJqiQVoYAAAeb5uXzTM377.png "title=" QQ picture 20160729103443.png "alt=" Wkiol1eawjqiqvoyaaaeb5uxztm377.png "/>


Conclusion:

The above content is my summary of ln command, because I beginner Linux, to this command understanding and understanding is not comprehensive and in-depth, inevitably there will be omissions, hope you pass the talent of many advice, greatly appreciated!

Summary of Linux hard links and soft 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.