Linux hard link and soft link details, linux link details

Source: Internet
Author: User

Linux hard link and soft link details, linux link details

In Linux, there are two special "Files": Soft links and hard links ".

Soft links can be seen as shortcuts in Windows, allowing you to quickly link to the target file or directory.
Hard links generate new file names through inode of the file system, instead of generating new files.

The creation method is simple:
Soft link (symbolic link) ln-s source target hard link (entity link) ln source target
Inode

To understand the differences and connection between the two, we need to first understand the inode in the linux File System. When partitioning and formatting a disk, the entire partition is divided into two parts: inode and data block (the actual data is placed in the data area ). This inode is the unique identifier of a file (directory, file) in a file system. To access this file, you must first find and read the inode of the file. Inode stores many important parameters of a file. The unique identifier is called Inumber. Other information includes the creation time (ctime) and modification time (mtime) file Size, owner, owner user group, read/write permission, data block number, and other information.

Inode quantity is usually arranged based on the partition usage (this is another topic). For example, if the number of files is large and the number of files is small, a large inode area is required, to index all files. Otherwise, the partition is not fully written and cannot be written to any file.

Directory files and archive files

Directory file: record the file name under this directory
Archive file: records the actual file data

Inode itself does not record the file name, and the file name is recorded in the block of the directory file. Therefore, adding, deleting, and changing the file name are related to the W permission of the directory. Therefore, when we want to read an archive, We must go through the inode and block in its directory before we can find the inode Number of the archive to be read, in the end, the data in the correct archive block will be read. The system locates each file by indexing nodes rather than file names.

Directory inode (permission satisfied ?) => Directory block => archive inode (permission satisfied ?) => Archive block
Hard Link

Multiple File Names correspond to the same inode. The hard link only adds a file name chain under a directory to the association record of an inode number. If you delete any file name, The inode and block of the file still exist. You can still use another file name to read the correct file data. In addition, no matter which file name is used for editing, the final result will be written into the same inode and block, so data can be modified.

Soft connection

A soft connection is used to create an independent file, which allows data to be read and pointed to the file name linked to it. Because it is only used as a pointing action, therefore, when the source file is deleted, the soft connection file cannot be enabled because the original file name cannot be found. The link file contains only the file name. The inode of the target file is obtained based on the link of the file name to the correct directory, so that the correct data can be read. If the original file name of the target file is deleted, the entire process will not be able to proceed.

The following uses an instance to describe hard links and soft links.

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

$ ls -il963922 -rw-r--r-- 1 ocean ocean 92 2007-05-18 15:46 AAA963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB

First, create a hard link for AAA:

$ ln AAA AAAhard$ls -il963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAA963922 -rw-r--r-- 2 ocean ocean 92 2007-05-18 15:46 AAAhard963923 -rw-r--r-- 1 ocean ocean 95 2007-05-18 15:46 BBB

Here, we note that before creating a link, AAA will display the number of links as 1. After creating a link:

The number of links between AAA and AAAhard is changed to 2; The inode numbers of AAA and AAAhard are the same, both of which are 963922; 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 to file AAA, creates a new pointer for the file index node of AAA in 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 pointers pointing to the file content are allowed, 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:

You cannot create hard links for directories. You cannot create links between files in different file systems. Because inode is the index value of this file in the current partition, it is relative to this partition, of course, it cannot span the file system.

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.

$ Ln-s BBB BBBsoft $ ls-il total usage 0963922-rw-r -- 2 ocean 92 AAA963922-rw-r -- 2 ocean 92 AAAhard963923-rw-r -- 1 ocean 95 BBB963924 lrwxrwxrwx 1 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:

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, it indicates that they are two different files. On the file attributes, the soft link clearly writes the link file, but the hard link is not written, in essence, hard-link files are completely equal to the original files. The number of links is different, and the number of soft-link links does not increase. The file size is different, the 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. Because BBB contains three characters in total, soft links do not have any restrictions on the file system. Any user can create symbolic links pointing to directories.

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 disadvantages that hard links do not have.: Because the link file contains the path information of the original file, when the original file is moved from one directory to another, and then the linked file is accessed, the system cannot find it, however, hard links do not have this defect. You need to move them as much as you want. In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path.

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.