Linux Soft connection/hard connection __linux

Source: Internet
Author: User
Tags mkdir

the difference between a hard link and a soft connection:

To illustrate this issue, first explain the nature of the files and directories under Liunx.

On the Liunx, the directory is also a file, which is a file that stores a table. For example, there is a folder called a program, which has two files 1 and 2. In that table of contents. It's the content of this

Name node

1 338

2 228

So what is a node? C language We have learned, we simply understand the node number as an array of subscript, the memory as a large array, each file can be seen as an element of an array, and know the node number, you can find the actual content of the file.

With the above understanding, you can further explain the hard links:

The hard link writing format is: ln target filename Link name

So what is the process of it?

For example we enter: LN 3 2

Then, add an item under the same table of contents

Name node

1 338

2 228

3 228

At this point, file 3 points to the same memory block as 2, which means that the contents of 2 are exactly the same.

And the soft connection is what's going on.

It must be noted that soft and hard links are also special files , in the Liunx are all in the file, soft connection can be regarded as a text file, its content is to save the destination file name path address.

Soft connection format is ln-s target filename link name

For example, enter Ln-s 4 2

Its execution is this, first copy the path name of 2 files to 4, perform 4 o'clock, first read the pathname from 2, find 2 of this file, and then execute 2. Therefore, the operation of 4 files is the operation of 2 files.

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, and when the file is accessed, the index nodes are copied to the inside to enable fast access to the file. Use command Ls-i to view node numbers

Hard link: (Hard link)

For files only, linked through index nodes, all files in the system will not have the same indoe. If there is the same indoe then the file is linked to another file, the content is the same and synchronized, called a hard link. You can use the LS–L command to display its number of hard links. Delete the source file, hard link file normal use.

Soft connection: (symbolic Link) also known as symbolic link

Files and directories can be, soft connection similar to Windows shortcuts, delete the source file, the soft connection will not be used normally. Note that a soft connection is a folder that, when entered into a directory under this soft connection, executes commands such as CP, and the directory is computed by source file directory, which is currently in the physical directory.

To Create a hard link: #ln source file Destination file

To create a soft connection: #ln –s source file destination file

1. Hard link

#mkdir/test

#cd/test

#touch Aaa.txt

#echo "I am System administrator" >> aaa.txt

#ln Aaa.txt/test/aaa.txt_hard_link

#ls-il

1144062-rw-r--r--2 root Feb 20:17 aaa.txt

1144062-rw-r--r--2 root Feb 20:17 Aaa.txt_hard_link

You can see that the index nodes of two files are identical, and the number of hard links is 2, the file itself is 1 hard links, plus 1 for each additional hard link, and the contents are exactly the same.

#mkdir BBB

#ln Bbb/test/bbb.txt_hard_link

ln: ' BBB ': Hard link not allowed for directory

Hard links are not able to manipulate the directory

If you delete the Aaa.txt source file, its hard link file Aaa.txt_hard_link works

2. Soft link

#ln –s Aaa.txt/test/aaa.txt_soft_link

#ls –il

1144062-rw-r--r--2 root Feb 20:17 aaa.txt

1144062-rw-r--r--2 root Feb 20:17 Aaa.txt_hard_link

1144065 lrwxrwxrwx 1 root 7 Feb 20:55 aaa.txt_soft_link-> aaa.txt

file type is L link file , you can use soft connection to file or directory, delete source file aaa.txt. Its soft connection file Aaa.txt_soft_link not working properly

Number of file Links: Create a new file its hard link number is 1, the file itself is a hard link.

Number of directory Links: directory hard link is 2 because the directory itself is a hard link , the directory of the. dot (current directory) is also a hard link,

Delete Link

RM-RF symbolic_name attention is not RM-RF symbolic_name/


Note: I node: it is the data structure within UNIX that describes the characteristics of the file. We usually call the I node the file index node (information node). The I node contains most of the important information about the file, including the address of the file data block on disk. Each I node has its own flag number, which we call the file order number. I node contains information 1. file type 2. File is the main relationship 3. Access rights for files 4. The time cut of the file.

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.