Soft links and hard links for Linux files

Source: Internet
Author: User

1.Linux Link Concept

There are two types of Linux links, one called hard links, and the other called Symbolic links (symbolic link). By default, the LN command produces a hard link.

1.1 Index nodes

An index node refers to a data structure in many Unix-like file systems. Each index node holds meta-information data for a file system object in the file system, but does not include data content or file names.

The inode contains meta information for the file, specifically the following:
* Number of bytes in the file
* User ID of the owner of the file
* The group ID of the file
* file read, write, execute permissions
* File timestamp, total three: CTime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, atime refers to the time when the file was last opened.
* Number of links, that is, how many filenames point to this inode
* Location of File data block
You can use the Stat command to view inode information for a file:
Stat Example.txt

* * Note: The **unix/linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify. On the surface, the user opens the file by file name. In fact, this process inside the system is divided into three steps: First, the system finds the inode number corresponding to the file name, and secondly, obtains the inode information through the inode number, and finally, according to the Inode information, finds the block of the file data and reads the data.

1.2 Hard connections

In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name. However, the Unix/linux system allows multiple filenames to point to the same inode number. This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. This is referred to as a "hard link".

A hard link is the same file that uses multiple aliases (they have a common inode). In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. That is, the condition that the file is actually deleted is that all the hard connection files associated with it are deleted.

1.3 Soft Links

Another connection is called a symbolic connection (symbolic link), also known as a soft connection. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

1.4 The difference between a hard link and a copy

A hard link simply refers to the file's Inode (the index node) through the file's alias, and the inode is the basis for the operating system to specify the file, each file has only one inode, so the operation of the hard link is the operation of the source file. Replication can be seen as creating a new file and copying the contents of the original file.

2. Deepen understanding through experimentation

[[email protected]] $ Touch F1 #创建一个测试文件f1
[[email protected]] $ LN F1 F2 #创建f1的一个硬连接文件f2
[[email protected]] $ Ln-s F1 F3 #创建f1的一个符号连接文件f3
[[email protected]] $ Ls-li #-I parameter displays inode node information for a file
Total 0
9797648-rw-r–r–2 Oracle Oinstall 0 APR 08:11 F1
9797648-rw-r–r–2 Oracle Oinstall 0 APR 08:11 F2
9797649 lrwxrwxrwx 1 Oracle oinstall 2 APR 08:11 f3-f1

As can be seen from the above results, the hard connection file F2 the same as the Inode node of the original file F1, all 9797648, however, the inode node of the symbolic connection file is different.

[[email protected]] $ echo "I am F1 file" >>f1
[[email protected]] $ Cat F1
I am F1 file
[[email protected]] $ Cat F2
I am F1 file
[[email protected]] $ Cat F3
I am F1 file
[[email protected]] $ Rm-f F1
[[email protected]] $ Cat F2
I am F1 file
[[email protected]] $ Cat F3
Cat:f3:No such file or directory

As you can see from the above test, hard connection F2 is not affected when the original file F1 is deleted, but the symbolic connection F1 file is not valid.

3. Summary

In this way you can do some related tests and get all the following conclusions:
(1) Remove the symbolic connection F3, no effect on F1,F2;
(2) Remove the hard connection F2, the F1,F3 also have no effect;
(3) Delete the original file F1, the hard connection F2 has no effect, resulting in symbolic connection F3 failure;
(4) While deleting the original file F1, hard connection F2, the entire file will be really deleted.
(5) A hard link is a different name of a file, a soft link is a hyperlink to a filename, the file name does not exist, and the hyperlink is invalidated.

Reference documents

[4] Index node. Baidu Encyclopedia

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Soft links and hard links for Linux files

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: 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.