A detailed explanation of soft links and hard links in Linux

Source: Internet
Author: User

Goal:

1. Test environment Simulation

2. Soft Link Features

3. Hard-Link features

4. Summary

1. Test environment Simulation


1

2

3

4

5

6

7

8

9

10

11

12

13

[[email protected] home]# mkdir test Create a testing folder

[[Email protected] home]# CD test/into the test folder

[[email protected] test]# Touch Link creates the original file link

[[email protected] test]# echo "My name is link" >>link write content to the original file link

[[email protected] test]# Cat Link View Original file contents

My name is link

[[email protected] test]# ln-s link softlink Create a soft link

[[Email protected] test]# LN link hardlink create a hard link

[email protected] test]# LL

Total 8

-rw-r--r--. 2 root root Dec 8 18:21 hardlink Hard link

-rw-r--r--. 2 root root 8 18:21 link Original file

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink link Soft Links


2. Soft Link Features


-rw-r--r--. 2 root root Dec 8 18:21 link Original file

&N BSP;

lrwxrwxrwx. 1 root root 4 Dec 8 18:22 softlink link Soft link

TD style= "BOX-SIZING:INHERIT; border-width:0px; border-style:initial; border-color:initial; background:0px Center; padding:0px!important; Vertical-align:top!important; margin:0px!important; "width=" 712 ">

1

2

3

Is there a few differences between the differences?

1. The original file Inode is 2 soft link 1

2. Different permissions

3. File size is different

4. There is a sign behind the soft link that points to link


1

[[email protected" test]# cat Softlink

my name is link

Soft link content is the same.


1

2

3

4

[Email protected] test]# RM softlink

Rm:remove Symbolic link ' softlink '? Y

[email protected] test]# Cat link

My name is link

Deleting the soft link original file is normal


1

2

3

4

[Email protected] test]# RM link

Rm:remove regular file ' link '? Y

[email protected] test]# cat Softlink

Cat:softlink:No such file or directory

Delete the original file Soft link can not find the file, the comprehensive proof that the soft link is a shortcut!!!

What happens if I change a soft link to a name?

1

2

3

4

6

7

[Email protected] test]# MV Softlink Testsoftlink

[email protected] test]# LL

Total 8

-rw-r--r--. 1 root root 8 18:36 link

lrwxrwxrwx. 1 root root 4 Dec 8 18:34 testsoftlink-Link

[email protected] test]# cat Testsoftlink

My name is link

The experiment proves that there is no egg to change the name, open the soft link can still see the content, why?

Because Linux recognizes a file without looking at the name, look at the Inode value!!!

This means that the Inode value is the same as the file content.

So the file can create a soft link, directory can?

1

2

3

4

5

[Email protected] test]# mkdir WJ

[Email protected] test]# ln-s WJ SOFTWJ

[email protected] test]# LL

lrwxrwxrwx. 1 root root 2 Dec 8 18:54 SOFTWJ-WJ

Drwxr-xr-x. 2 root root 6 Dec 8 18:54 WJ

Catalogs can create soft links

3. Hard-Link features


1

2

3

4

6

[email protected] test]# LL

Total 8

-rw-r--r--. 2 root root Dec 8 18:36 hardlink

-rw-r--r--. 2 root root 8 18:36 link

[email protected] test]# cat Hardlink

My name is link

See that a hard link is a backup of the original file.


1

2

3

4

[Email protected] test]# RM link

Rm:remove regular file ' link '? Y

[email protected] test]# cat Hardlink

My name is link

Delete the original file, hard link is can see the content, so. This is one of the differences with soft links.

so can a hard link create a directory link like a soft link?

[[email protected" test]# mkdir CS

[[email protected] test]# LN cs Hardcs

LN: ' cs ': Hard link not allowed for director Y

TD style= "BOX-SIZING:INHERIT; border-width:0px; border-style:initial; border-color:initial; background:0px Center; padding:0px!important; Vertical-align:top!important; margin:0px!important; "width=" 712 ">

1

2

3

No, you can't. Why is it?

Because of that unique value! What if the directory inode is the same?

When accessing the soft link, a soft link directly jumps to the original file, thus accessing the content

Access to the soft-link directory by traversing the contents of the directory can also be found, even if the folder Inode value has the same loop, Linux can be in 8 cycles to end.

But

If our hard link is accessed, the original file is not changed and it has nothing to do with it.

Our hard link if there is a hard-link directory, then the traversal of the contents of the same inode value in the same directory, all over again, loop at least in the directory of the Linux system can not be terminated, so the hard-link directory is not created drip!!!

4. Summary

Soft links like shortcuts, the original file content changed soft links will also change, affecting the file is not the name but the Inode value, soft links can create a soft link directory.

Hard links like backups, the original file content changes do not affect hard links, so usually work as a snapshot to use, hard links do not have a hard-link directory.


A detailed explanation of soft links and hard links in Linux

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.