Go: Linux File links (soft links and hard links)

Source: Internet
Author: User

Link: A method of establishing a connection between a shared file and a number of directory entries for the user who accesses it.

There are two types of links in Linux: Hard links and soft links (Soft link), and soft links called Symbolic links (symbolic link).

Let's first look at a few nouns:

Index node (inode)

To understand the link, we first need to understand a concept called an index node (inode). In a Linux system, the kernel allocates an Inode (index node) for each newly created file, each with a unique inode number, and we can simply interpret the inode as a pointer that will always point to the specific storage location of this file. The file attributes are stored in the index node, and the index nodes are copied to the file when they are accessed, enabling quick access to the file. The system locates each file through an index node rather than a file name.

Hard link

Hard links plainly is a pointer to a file index node, and the system does not redistribute the 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:

The first creates a hard link for "existingfile", and the file name is "NewFile".

The second in the directory directory, create a hard link with the same name for all files contained in "Existingfile-list".

Common optional [options]:-f the link is created regardless of whether the newfile exists or not. -N If "NewFile" already exists, no link is created.

Soft links (Soft link)

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.

You can use the: ln-s command to create a soft link.

Reference

Ln-s Existingfile NewFile

Ln-s existingfile-list Directory

Below we use an example to illustrate hard links and soft links.

Now there are two files in the directory, one named File1, and one named File2.

Reference

$ ls–il

1052671-rw-r--r--1 leanx leanx 0 2010-03-29 13:46 file1

1052670-rw-r--r--1 leanx leanx 0 2007-03-29 13:46 file2

First make a hard link to file1.

Reference

$ ln file1 File1hard

$ls –il

1052671-rw-r--r--2 leanx Leanx 0 2010-03-29 13:46 file1

1052670-rw-r--r--1 leanx leanx 0 2010-03-29 13:46 file12

1052671-rw-r--r--2 leanx Leanx 0 2010-03-29 13:46 File1hard

Here we note that before the link is created, the number of links displayed by File1 is 1, after the link is created

The number of links between 1.file1 and File1hard is changed to 2.

2.file1 and File1 have the same inode number, which is 1052671.

3.file1 and File1hard show the same file size, which is 0 B.

The operation results of the LN command are visible: File1 and File1hard are two names of the same file, they have the same index node number and file attributes, and a hard link to the file file1 is to create a new pointer to the File1 file index node in the current directory. You can delete any one of them, such as RM file1, and delete only one pointer at a time, minus one at a time, and the kernel will delete the contents of the file from the disk only if all pointers to the contents of the file, that is, the number of links is reduced to 0 o'clock.

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.

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

Then we do a soft link to file2, 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, it has greater flexibility, and can even link files across different machines and networks.

Reference

$ ln-s file2 File2soft

$ ls–il

Total dosage 0

1052671-rw-r--r--2 leanx Leanx 0 2010-03-29 13:34 file1

1052670-rw-r--r--1 leanx leanx 0 2010-03-29 13:35 file2

1053313 lrwxrwxrwx 1 Leanx leanx 5 2010-03-29 13:45 File2soft-file2

1052671-rw-r--r--2 leanx Leanx 0 2010-03-29 13:34 File1hard

From the results of the link above can be seen in soft links and hard links, the difference is not only conceptually, in the implementation is also completely different.

Difference:

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 file2 size is 0B, and the File2soft is 5 B.

In short, creating a soft link is the creation of a new file. When accessing a linked file, the system will find that he is a link file that reads the link file to find the file that is actually being accessed.

Of course, soft links also have the disadvantage of hard links, because the link file contains the path information of the original file, so when the original file from one directory to another directory, and then access the linked file, the system will not be found, and the hard link does not have this flaw, you want to move how to move And it wants the system to allocate additional space for creating a new index node and saving the path to the original file.

Go: Linux File links (soft links and hard links)

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.