Linux Course notes soft and hard links

Source: Internet
Author: User

1. Introduction and practice of soft and hard Links 1.1 link concept

In Linux systems, links are broken down into hard links and soft links. With the default without parameters, the Execute ln command creates a hard link.

A link is a link that is linked through an index node. In a Linux system, it is normal and permissible for multiple filenames to point to the same index node, which is a hard link. One of the functions of a hard link is to allow a file to have multiple valid paths to prevent accidental deletion of source data.

File system, as long as the file's index node has more than one link, deleting only one of the links does not affect the index node and other links (that is, the data entity is not deleted). Only if the last link is deleted, if there is new data to be stored on the hard disk, the data block of the deleted file and the link of the directory will be released.

A soft link is actually a text file that contains the location information content of a soft link pointing to a supernatural file.

1.2 Example

Examples of 1.2.1 files:

[Email protected] test]# mkdir-p/TEST/QINBF

[Email protected] test]# cd/test/qinbf/

[email protected] qinbf]# Touch QINBF

[email protected] qinbf]# LL

Total 4

-rw-r--r--1 root root 0 05-27 12:21 qinbf #------à have not created a link at this time, the number of links is 1

[[Email protected] qinbf]# ln qinbf qinbf_hard_link #----------à no arguments, create hard links by default

[email protected] qinbf]# LL

Total 8

-rw-r--r--2 root root 0 05-27 12:21 qinbf #-----------à at this time due to the existence of a hard link, the number of links is 2

-rw-r--r--2 root root 0 05-27 12:21 qinbf_hard_link

[Email protected] qinbf]# ln-s QINBF qinbf_soft_link

[email protected] qinbf]# LL

Total 12

-rw-r--r--2 root root 0 05-27 12:21 QINBF

-rw-r--r--2 root root 0 05-27 12:21 qinbf_hard_link

lrwxrwxrwx 1 root root 5 05-27 12:22 qinbf_soft_link QINBF

[Email protected] qinbf]# Ll-rti

Total 24

1409027-rw-r--r--2 root root 0 05-27 12:21 qinbf_hard_link

1409027-rw-r--r--2 root root 0 05-27 12:21 QINBF #---à on edge, found that the QINBF file and its hard-linked file inode number are consistent

1409028 lrwxrwxrwx 1 root root 5 05-27 12:22 qinbf_soft_link-QINBF #------à qinbf file's soft link file qinbf_soft_link inode number Inconsistent with QINBF file inode number

1409029 drwxr-xr-x 2 root root 4096 05-27 12:22 Qinbfdir

1409030 lrwxrwxrwx 1 root root 8 05-27 12:23 Qinbfdir_soft_link-Qinbfdir

1.2.2 Directory Example:

[Email protected] qinbf]# mkdir Qinbfdir

[email protected] qinbf]# LL

Total 20

-rw-r--r--2 root root 0 05-27 12:21 QINBF

Drwxr-xr-x 2 root root 4096 05-27 12:22 Qinbfdir

-rw-r--r--2 root root 0 05-27 12:21 qinbf_hard_link

lrwxrwxrwx 1 root root 5 05-27 12:22 qinbf_soft_link QINBF

[Email protected] qinbf]# LN qinbfdir qinbfdir_hard_link

ln: "Qinbfdir": Do not allow hard links to be directed to directory #--------à directory does not allow hard links to be created

[[email protected] qinbf]# ln-s qinbfdir qinbfdir_soft_link #----------à directory can create soft links

[Email protected] qinbf]# Ll-rti

Total 24

1409027-rw-r--r--2 root root 0 05-27 12:21 qinbf_hard_link

1409027-rw-r--r--2 root root 0 05-27 12:21 QINBF

1409028 lrwxrwxrwx 1 root root 5 05-27 12:22 Qinbf_soft_link-QINBF

1409029 drwxr-xr-x 2 root root 4096 05-27 12:22 Qinbfdir

1409030 lrwxrwxrwx 1 root root 8 05-27 12:23 Qinbfdir_soft_link-Qinbfdir

1.2.3 Content Input Example:

[Email protected] qinbf]# echo "QINBF is a student" >>qinbf_soft_link

[email protected] qinbf]# cat Qinbf_soft_link

QINBF is a student

[email protected] qinbf]# cat Qinbf_hard_link

QINBF is a student

[email protected] qinbf]# cat QINBF

QINBF is a student

[Email protected] qinbf]# echo "Yes,i do" >>QINBF

[email protected] qinbf]# cat QINBF

QINBF is a student

Yes,i do

[email protected] qinbf]# cat Qinbf_hard_link

QINBF is a student

Yes,i do

[email protected] qinbf]# cat Qinbf_soft_link

QINBF is a student

Yes,i do

Conclusion: The contents of these three types of files are synchronized whether they are in a soft link file or a hard-link file or a source file.

1.2.4 Delete Example:

[Email protected] qinbf]# rm-f qinbf #-----------à delete source file

[Email protected] qinbf]# Ll-rti

Total 24

1409028 lrwxrwxrwx 1 root root 5 05-27 12:22 Qinbf_soft_link qinbf[q1]

1409029 drwxr-xr-x 2 root root 4096 05-27 12:22 Qinbfdir

1409030 lrwxrwxrwx 1 root root 8 05-27 12:23 Qinbfdir_soft_link-Qinbfdir

1409027-rw-r--r--1 root root 05-27 12:29 Qinbf_hard_link

[email protected] qinbf]# Cat Qinbf_soft_link #------à this time, the soft link file fails

Cat:qinbf_soft_link: No file or directory

[email protected] qinbf]# Cat qinbf_hard_link #------à hard-link file content is still in

QINBF is a student

Yes,i do

============================= below is your own test supplement ===========================

[[email protected] qinbf]# Touch QINBF #----------à previously deleted, re-created new file with the same name

[Email protected] qinbf]# Ll-rti

Total 28

1409028 lrwxrwxrwx 1 root root 5 05-27 12:22 Qinbf_soft_link QINBF[Q2]

1409029 drwxr-xr-x 2 root root 4096 05-27 12:22 Qinbfdir

1409030 lrwxrwxrwx 1 root root 8 05-27 12:23 Qinbfdir_soft_link-Qinbfdir

1409027-rw-r--r--1 root root 05-27 12:29 Qinbf_hard_link

1409031[Q3]-rw-r--r--1 root root 0 05-27 12:33 QINBF

[[email protected] qinbf]# Cat Qinbf_soft_link #------Àqinbf_soft_link file and new file QINBF content consistent, empty

[[email protected] qinbf]# echo "This is new file" >>QINBF #-----à added content

[[email protected] qinbf]# Cat Qinbf_soft_link #-------Àqinbf_soft_link file and new file QINBF content consistent

This is new file

[[email protected] qinbf]# Cat Qinbf_hard_link #------à the contents of the QINBF file at this time Qinbf_hard_link file or deleted

QINBF is a student

Yes,i do

1.3 Link Summary

Through the above tests, we can get the following conclusions:

(1) Delete soft link file, no effect on source file and hard link file

(2) Delete hard-link files, no impact on source files and soft links

(3) Delete source files, no impact on hard-link files, soft link invalidation

(4) Delete the source file at the same time, and the hard link file, the whole file will be really deleted

(5) The snapshot function of many hardware devices is to take advantage of the principle of hard link

(6) Create a new file with the same name as the deleted source file, the soft link file is still valid, and the hard link is invalid (the inode number is inconsistent), the pro-test.

For a directory, you cannot create a hard link, but you can create a soft link; the directory cannot span the file system.

1.4 Reasons why the space cannot be released

(1) The existence of hard links

(2) Another process is still using this file

(3) There is a problem with the disk remaining space maintenance

Linux Course notes soft 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.