Hard link and soft link (2)

Source: Internet
Author: User
Tags symlink

Statement: This article is reposted from Workshop.

 

First, you need to understand that in Linux, the kernel assigns an inode (index node) to each newly created file, and each file has a unique inode number. The file attributes are stored in the index node. when accessing the file, the index node is copied to the inner to implement fast file access.

A link is a way to establish 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 ).

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

ln [options] existingfile newfileln[options] existingfile-list directory</CCID_CODE />

</Ccid_nobr/>

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

The following are some examples:

$ LS-il13058-rwx--1 longcheng 48 August 8 16:38 file113059-rwx---1 longcheng 57 August 17 16:40 file2 $ ln file2 file2hard $ ls -il13058-rwx--1 longcheng 48 August 8 16:38 file113059-rwx---2 longcheng 57 August 7 16:40 file213059-rwx ------ 2 longcheng 57 Aug 17 16:40 file2hard </ccid_code/>

</Ccid_nobr/>

Note: before creating a link, the number of links displayed in file1 is 1. After the link is created, (1) the number of links in file1 and file1hard is changed to 2. (2) file1 and file1hard are the same in inode (3) the file size displayed by file1 and file1hard is the same. The result of LN command is as follows: file1 and file1hard are two names of the same file. They have the same index node number and file attributes and create a hard link to file1, creates a new pointer for the file index node of file1 in the current directory. For example, you can delete any of them, such as RM.
File2, each time only one pointer is deleted,

The number of links is reduced by one at the same time. The kernel will delete the file content from the disk only when all the pointers pointing to the file content, that is, the number of links is reduced to 0. The logical structure of the current directory: (Sorry, the figure is not shown ).

You can also create hard links to files in different directories but in the same file system. Set file1 and file2 to the/home/longcheng/dir1 directory. Run the following command to create a hard link for file2 in/home/longcheng.

ln file2 /home/longcheng/file2hard</CCID_CODE />

</Ccid_nobr/>

The following program creates a hard link for all the files in the dir1 directory in the directory dir2.

$mkdir dir2$ln /home/longcheng/dir1/* /home/longcheng/dir2</CCID_CODE />

</Ccid_nobr/>

If ln-F existingfile newfile is used and newfile already exists, newfile becomes a hard link file of exisitngfile no matter what the original newfile is, and only the current user has the write permission on it.

Although hard links save space, they are also the traditional way to integrate file systems in Linux systems, but there are some shortcomings: (1) You cannot establish links between files in different file systems (2) only Super Users can create hard links for directories. Although many trees say that root users can be created, I found that even root users cannot be created during the learning process. My system is RedHat and I have tried both kernel 2.4 and 2.6, in other systems, you do not know if it is OK.
 

Ii. Soft link (symbolic link)

Soft links overcome hard links without any restrictions on the file system. Any user can create symbolic links pointing to directories. As a result, it is more widely used. It has more flexibility and can even link files across different machines and networks.

To create a soft link, add the option-s After LN. The following is an example:

$ LS-il13058-rwx--1 longcheng 48 August 8 16:38 file113059-rwx---2 longcheng 57 August 17 16:40 file213059-rwx ---- --2 longcheng 57 16:40 file2hard $ ln-s file1 file1soft $ LS-il13058-rwx--1 longcheng 48 16:38 file113059-rwx ---- --2 longcheng 57 August 5 16:40 file213059-rwx--2 longcheng 57 August 5 16:40 file2hard13061 minutes 1 longcheng 5 August 5 16:58 file1soft-> file1 </ccid_code/>

</Ccid_nobr/>

From the above link results, we can see that soft links and hard links are different not only in terms of concept, but also in terms of implementation. Difference: A Public inode number is used for hard-link files and linked files, indicating that they are the same file, while soft-link files and linked files have 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. This is emphasized because it is equivalent, and the size of the soft link is different from that of the original file, the size of file1 is 48B, while that of file1soft is 5B, where 5 is actually the size of "file1.

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.

Creating soft links between different systems and creating links to directories is not an example here. You can try it on your own. I am also learning it in practice.

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 cannot find it ~~, Hard links do not have this defect, so you need to move them as much as you want (haha). In addition, the system needs to allocate additional space for creating new index nodes and saving the original file path.

Supplement: You can view the link file through symlink, and you can use man symlink to learn.

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.