Example to explain the creation of hard link and soft link in Linux system

Source: Internet
Author: User
Tags symlink

The first thing to figure out is that in a Linux system, 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, and when the file is accessed, the index nodes are copied to the inside to enable fast access to the file.
A link is a way to establish a connection between a shared file and a number of directory entries for the user who accesses it. Linux includes two kinds of links: Hard link (Hard link) and soft link (Soft link), soft link is also known as symbolic link (symbolic link).

One, Hard link
Hard links plainly is a pointer to the file index node and the system does not reassign the inode for it. You can use the LN command to establish a hard link. Grammar:

The code is as follows:

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

Usage: First: Create a hard link for "Existingfile", the file name is "NewFile". Second: In the directory, create a hard link with the same name for all files contained in "Existingfile-list". Common optional [options]–f create links regardless of whether "newfile" exists or not. -N If "NewFile" already exists, the link is not created.
Here are some examples:

The code is as follows:

$ ls–il

13058-RWX-----1 Longcheng Longcheng 48 August 5 16:38 File1
13059-RWX-----1 Longcheng Longcheng 57 August 5 16:40 File2

The code is as follows:

$ ln file2 File2hard
$ ls–il

13058-RWX-----1 Longcheng Longcheng 48 August 5 16:38 File1
13059-RWX-----2 Longcheng Longcheng 57 August 5 16:40 File2
13059-RWX-----2 Longcheng Longcheng 57 August 5 16:40 File2hard
Note the number of links that File1 displays is 1 before the link is created, and the number of links between File1 and File1hard is 2 after the link is created (2) file1 and File1hard are the same in the Inode number (3) The same file size is displayed by File1 and File1hard. You can see the results of the LN command: File1 and File1hard are two names of the same file, they have the same index node number and file attributes, establish a hard link to the file file1, or create a new pointer to the File1 file index node in the current directory.
The number of links minus one at a time, the kernel will remove the contents of the file from disk only if all pointers to the contents of the file, or the number of links, are reduced to 0 o'clock. Current directory Logical Structure: (sorry, the diagram is not shown).
You can also create hard links to files in different directories, but in the same file system. Set File1, file2 in the directory/home/longcheng/dir1, the following command to establish a file2 hard link in/home/longcheng.
The code is as follows:

ln File2/home/longcheng/file2hard


The following procedure is to create a hard link in the directory Dir2 for all files in the Dir1 directory

The code is as follows:

$mkdir Dir2
$LN/home/longcheng/dir1/*/home/longcheng/dir2

If the ln–f existingfile newfile is used, if newfile already exists, then no matter what file the original NewFile is, only the current user has write access to it, and NewFile becomes the hard link file for Exisitngfile.
Although hard links save space and are the traditional way for Linux systems to consolidate file systems, there are drawbacks:
(1) No links can be established between files in different file systems
(2) Only Superuser can create a hard link for the directory. Although many trees say that the root user can create, but the author in the learning process found that even the root user could not create, my system is redhat, the kernel 2.4, 2.6 have tried, in other systems do not know whether it can.

Second, soft link (symbolic link)
Soft links overcome the lack of hard links, no file system restrictions, any user can create a symbolic link to the directory. So now it's more widely used, it has more flexibility, and can even link files across different machines and networks.
To create a soft link, just add the option –s to the LN, and here's an example

The code is as follows:

$ ls-il

13058-RWX-----1 Longcheng Longcheng 48 August 5 16:38 File1
13059-RWX-----2 Longcheng Longcheng 57 August 5 16:40 File2
13059-RWX-----2 Longcheng Longcheng 57 August 5 16:40 File2hard

The code is as follows:

$LN –s file1 File1soft
$ls-il

13058-rwx-----1 Longcheng Longcheng 48 August 5 16:38 file1
13059-rwx-----2 Longcheng Longcheng 57 August 5 16:40 file2
13059-rwx----2 Longcheng Longcheng 57 August 5 16:40 File2hard
13061 lrwxrwxrwx 1 Longcheng L Ongcheng 5 August 5 16:58 file1soft->file1
The results from the link above can be seen in soft links and hard links, the difference is not only in the concept, in the implementation is also different. Difference: Hard Link Original file & Link file common an inode number, stating that they are the same file, and that the soft link original file and link file have different inode numbers, indicating that they are two different files; the soft links on the file properties explicitly write the link file, and the hard link is not written, Because in essence the hard link file and the original file are completely equal; The number of links is different, the number of links to soft links does not increase; File size is not the same, hard link file display size is the same as the original file, which is emphasized, because it is equivalent, and here the soft link display size and the original file is different, The file1 size is 48B, and the File1soft is 5B, and the 5 is actually the size of "file1".
In summary, creating a soft link creates a new file. When you access the linked file, the system finds that he is a linked file that reads the linked file and finds the file that you really want to access.
to establish soft links between different systems, link to the directory, there is no example here, the reader can try for themselves, I am also in the continuous practice of learning.
Of course soft links also have no disadvantages of hard links, because the link file contains the original file path information, so when the original file from a directory moved to other directories, and then access to the link file, the system can not find ~ ~, and hard links do not have this flaw, you want to move on how to move (hehe) And it wants the system to allocate additional space for creating new indexing nodes and for saving the path of the original file.
Add: You can view linked files through Symlink, and you can learn with man symlink.

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.