Ln usage, knowledge (soft link and hard link)

Source: Internet
Author: User

Differences between soft links and hard links in Linux

For a long time, Linux connections have been a little ignorant, and there are also some problems in use. These two days, I carefully checked the information on the Internet and carefully understood the hard links and soft links on Linux.

Link --------- is a method that establishes 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 ).

First, let's take a look at several terms.

Inode)
To understand the link, we must first understand the concept of an index node (inode ). In Linux, the kernel allocates an inode (index node) to each newly created file. Each file has a unique inode number. We can simply think of inode as a pointer, it always points to the specific storage location of the file. 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. The system locates each file by indexing nodes rather than file names.

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.

Reference ln [Options] existingfile newfile
Ln [Options] existingfile-List Directory

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

Soft link)
Soft links are also called symbolic links. This file contains the path name of another file. It can be any file or directory that can be linked to files of different file systems. It is similar to the shortcut in win.
You can use the: ln-s command to create a soft link.

Reference ln-s existingfile newfile
Ln-s existingfile-List Directory

Next we will use an instance to describe the hard link and soft link.

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

Reference $ LS-il
963922-RW-r -- 1 Ocean ocean 92 aaa
963923-RW-r -- 1 Ocean ocean 95 bbb

First, create a hard link for AAA.

Reference $ ln AAA aaahard
$ LS-il
963922-RW-r -- 2 ocean 92 aaa
963922-RW-r -- 2 ocean 92 aaahard
963923-RW-r -- 1 Ocean ocean 95 bbb

Note that before creating a link, AAA displays the number of links as 1. After creating a link
1. The number of connections between AAA and aaahard is changed to 2.
2. The inode numbers of AAA and aaahard are the same, both of which are 963922.
3. 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:
1. Hard links cannot be created for directories.
2. You cannot create links between files in different file systems.

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.

Reference $ ln-s BBB bbbsoft
$ LS-il
Total usage 0
963922-RW-r -- 2 ocean 92 aaa
963922-RW-r -- 2 ocean 92 aaahard
963923-RW-r -- 1 Ocean ocean 95 bbb
963924 lrwxrwxrwx 1 Ocean 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:
1. 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, indicates that they are two different files;
2. The soft link on the file property clearly writes the link file, but the hard link is not written, because in essence the hard link file is completely equal to the original file;
3. The number of links is different. The number of soft links does not increase;
4. The file size is different. The displayed 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.
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 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 won't be able to find it, and the hard link won't have this defect. You can move it as you want; it also requires the system to allocate additional space for creating new index nodes and saving the original file path.

Connection count:
As we have discussed earlier, the file link counting field indicates that there are several file directories in the system with the same inode as this file, that is, there are several hard links in this file. In the preceding example, the Count values of the files AAA and aaahard are both 2.

So what is the meaning of the count field of a directory? The count of the directory also indicates how many directory items direct to this directory. However, to be detailed, you must further explain the structure of the VFS file system. For simplicity, as long as this understanding is done: (count-2) equals to the number of direct subdirectories contained in the directory (that is, only the son, not the grandson ).

For example, if the Count field of a directory/ln is 5, the/ln directory must contain three subdirectories.
/*************************************** ******************

Understand the structure of the file system before learning ln:

Node Zone data Zone
-------------------
|
|
|
.......
| ------- | -------- |
Such a file system is like a book. The node area corresponds to the book directory, and the data area corresponds to the formal chapter of the book.
This document uses the/etc/passwd file as an example.
[Applprod @ ERP applprod] $ STAT/etc/passwd
File: "/etc/passwd"
Size: 1603 blocks: 8 Io block:-4612090707425882112 regular file
Device: 803 H/2051d inode: 224278 links: 1
Access: (0644/-RW-r --) uid: (0/root) gid: (0/root)
Access: Mon Oct 23 15:29:34 2006
Modify: Mon Jul 24 07:49:15 2006
Change: Mon Jul 24 07:49:15 2006

1. ln is divided:
1) Hard Link (node backup );
2) Symbolic Link (soft link, similar to the shortcut in Win );

2. Scope of use
1) hard links can be created in any shard. There is no limit on the number of hard links. directories and cross-partitions are not supported. A hard link is equivalent to two different pointers pointing to the data zone with the same page number. you can use hard links to back up data (actually a backup node). However, if there is a problem in the Data zone, there is no way to back up data.
For example, ls file is okay, but Cat file is faulty.

2) The Symbolic Link is used to create a new node (soft node --> hard node --> data zone) pointing to a node in the Data zone. It supports directory links and supports cross-partitioning, there is a limit on the number of links (preferably no more than 8 times). If the original file is deleted (soft node --> hard knot point --> data zone), the symbolic link becomes a broken link.

**************************************** *******************/

Ln command usage

Command name: ln
Permission: All Users
Usage: ln [Options] source Dist, where the option format is:
[-Bdfinsvf] [-s backup-suffix] [-V {numbered, existing, simple}]
[-- Help] [-- version] [--]

Note: There is a link in the Linux/Unix File System. we can regard it as a different object.
And can be divided into two types: hard link and symbolic link.
A file can have multiple names, while a soft link generates a special file.
The content is the location that points to another file. Hard links exist in the same file system, but soft links can span
Different file systems.
Ln source dist is used to generate a connection (DIST) to the source. As for hard link or soft link, the parameter
Decide.
No matter whether it is a hard link or soft link, it will not copy the original file, it will only occupy a very small amount of disk space.

Parameters:
-F: when the link is closed, the file with the same name as dist is deleted first.

-D: allows system administrators to hard link their directories.

-I: When deleting an archive with the same name as Dist, first ask

-N: dist is considered as a normal file During Soft link.

-S: Symbolic Link)

-V: display the file name before the link

-B: Back up the files that will be overwritten or deleted during the link.

-S Suffix: adds the backup file to the end of suffix.

-V method: Specifies the backup method.

-- Help: displays auxiliary instructions

-- Version: displays the version.

Example:
Generate a symbolic link: ZZ
Ln-s YY ZZ
Generate a hard link: ZZ
Ln YY xx

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.