Hard connection and soft connection file in UNIX

Source: Internet
Author: User
Tags lstat

If you find that the created symbolic link cannot be deleted, you can only find that the file name cannot be suffixed with one/after the symbolic link is deleted /. For example:
[WHB @ jcwkyl test] $ ln-S ~ WHB

[WHB @ jcwkyl test] $ ls

WHB

[WHB @ jcwkyl test] $ rm whb/

RM: cannot remove 'whb/': Not a directory

[WHB @ jcwkyl test] $ Rm-f whb/

RM: cannot remove 'whb/': Not a directory

[WHB @ jcwkyl test] $ RM WHB

[WHB @ jcwkyl test] $ ls

Review the concepts of hard connection and symbolic connection:
Each file in Unix has a data structure associated with it to describe the information of the file. The data structure is inode.
For example, to create a file:
[WHB @ jcwkyl test] $ ls

[WHB @ jcwkyl test] $ cat> testfile <EOF

> This is just an example!

> EOF

The file name testfile points to an inode data structure. You can run the STAT command to view the inode information:
[WHB @ jcwkyl test] $ stat testfile

File: 'testfile'

Size: 25 blocks: 8 Io block: 4096 regular file

Device: 802 H/2050d inode: 9306116 links: 1

Access: (0664/-RW-r --) uid: (500/WHB) gid: (500/WHB)

Access: 16:16:27. 000000000 + 0800

Modify: 16:16:27. 000000000 + 0800

Change: 16:16:27. 000000000 + 0800

The inode number of the testfile is 9306116. In gnu c, you can use the stat, fstat, and lstat functions to view the information of a file. The difference between them is that if you are viewing a symbolic connection file, lstat displays the information of the symbolic connection file, while fstat displays the information of the file pointed to by the symbolic connection file. When you use fopen to open a symbolic connection file, the operation is actually applied to the file pointed to by the symbolic connection file.

Create a hard connection file pointing to the testfile, which means to create a file name that points to the inode pointed to by testfile. Use the link command to create a hard connection file:
[WHB @ jcwkyl test] $ link testfile hardlink

[WHB @ jcwkyl test] $ stat hardlink

File: 'hardlink'

Size: 25 blocks: 8 Io block: 4096 regular file

Device: 802 H/2050d inode: 9306116 links: 2

Access: (0664/-RW-r --) uid: (500/WHB) gid: (500/WHB)

Access: 16:16:27. 000000000 + 0800

Modify: 16:16:27. 000000000 + 0800

Change: 16:23:20. 000000000 + 0800

As you can see, the type of the hardlink file is regular file. Its inode number is exactly the same as the inode Number of testfile. Its links is 2, which is a reference count of inode. When you delete a hardlink or testfile, its links will be reduced to 1. If you delete the remaining file, its links will become 0, in this case, the operating system will delete the file from the disk.
The file name is equivalent to a pointer, which points to an inode, And the directory is equivalent to a second-level pointer, which points to a list of file names. You cannot create a hard connection to a directory.

Creating a hard connection is not equal to copying a file. Copying a file copies the content on the disk. Creating a hard connection is just a pointer. The common difference between "copying a file" and "Creating a hard connection to a file" is that the source file must exist. Therefore, you cannot create hard connections across file systems or partitions. For example:
[WHB @ jcwkyl test] $ DF-H

Filesystem size used avail use % mounted on

/Dev/sda3 49G 9.2g 37g 20%/

/Dev/sda2 49G 11G 36g 24%/home

/Dev/sda1 99 m 19 m 75 m 21%/boot

Tmpfs 1013 M 0 1013 M 0%/dev/SHM

/Dev/sda6 191g 36g 146g 20%/Vm

My/home is/dev/sda2,/boot is on/dev/sda1, and a hard connection to a file under/boot is created:
[WHB @ jcwkyl test] $ link/boot/system. map-2.6.18-92.el5 Map

Link: cannot create link 'map' to '/boot/system. map-2.6.18-92.el5': Invalid cross-device link

Error because no system. map-2.6.18-92.el5 map file is available on/dev/sda2.

Similarly, hard connections do not occupy disk space because creating hard connections only creates a pointer. The above command uses the STAT command to view the testfile and hardlink files, reading the same inode information, so the size is naturally the same, but you can use the du command to view the information, it automatically calculates only one copy of multiple hard Connection Files pointing to the same inode:
[WHB @ jcwkyl test] $ Du-HS *

4.0 K hardlink

[WHB @ jcwkyl test] $ ls

Hardlink testfile

Use another example for verification. In the output of DF-H above, we can see that there is still GB space in the/dev/sda6 partition and enter this partition for an experiment:
[WHB @ jcwkyl test] $ CD/Vm

[WHB @ jcwkyl VM] $ Du-HS *

5.1g centos. VDI

Du: cannot read directory 'lost + found ': Permission denied

1.5g rh9img. VDI

54 m share

8.1g vm1_clone.img

8.1g vm1_convert.img

987 m vm1.vdi

936 M vm2.vdi

937 m vm3.vdi

1.6g winxp_slave.vdi

7.4g WINXP. VDI

1.6g xen-Images

We can see that the WINXP. VDI file contains 7.4 GB. We will create 100 hard connections for this file:
[WHB @ jcwkyl VM] $ for (I = 1; I <= 100; I ++); Do link WINXP. vdi winxp. VDI _ $ I; done

[WHB @ jcwkyl VM] $ ls

Centos. vdi winxp. vdi_13 WINXP. vdi_28 WINXP. vdi_42 WINXP. vdi_57 WINXP. vdi_71 WINXP. vdi_86

Lost + found WINXP. vdi_14 WINXP. vdi_29 WINXP. vdi_43 WINXP. vdi_58 WINXP. vdi_72 WINXP. vdi_87

Rh9img. vdi winxp. vdi_15 WINXP. vdi_3 WINXP. vdi_44 WINXP. vdi_59 WINXP. vdi_73 WINXP. vdi_88

Share WINXP. vdi_16 WINXP. vdi_30 WINXP. vdi_45 WINXP. vdi_6 WINXP. vdi_74 WINXP. vdi_89

Vm1_clone.img WINXP. vdi_17 WINXP. vdi_31 WINXP. vdi_46 WINXP. vdi_60 WINXP. vdi_75 WINXP. vdi_9

Vm1_convert.img WINXP. vdi_18 WINXP. vdi_32 WINXP. vdi_47 WINXP. vdi_61 WINXP. vdi_76 WINXP. vdi_90

Vm1.vdi WINXP. vdi_19 WINXP. vdi_33 WINXP. vdi_48 WINXP. vdi_62 WINXP. vdi_77 WINXP. vdi_91

Vm2.vdi WINXP. vdi_2 WINXP. vdi_34 WINXP. vdi_49 WINXP. vdi_63 WINXP. vdi_78 WINXP. vdi_92

Vm3.vdi WINXP. vdi_20 WINXP. vdi_35 WINXP. vdi_5 WINXP. vdi_64 WINXP. vdi_79 WINXP. vdi_93

Winxp_slave.vdi WINXP. vdi_21 WINXP. vdi_36 WINXP. vdi_50 WINXP. vdi_65 WINXP. vdi_8 WINXP. vdi_94

WINXP. vdi winxp. vdi_22 WINXP. vdi_37 WINXP. vdi_51 WINXP. vdi_66 WINXP. vdi_80 WINXP. vdi_95

WINXP. vdi_1 WINXP. vdi_23 WINXP. vdi_38 WINXP. vdi_52 WINXP. vdi_67 WINXP. vdi_81 WINXP. vdi_96

WINXP. vdi_10 WINXP. vdi_24 WINXP. vdi_39 WINXP. vdi_53 WINXP. vdi_68 WINXP. vdi_82 WINXP. vdi_97

WINXP. vdi_100 WINXP. vdi_25 WINXP. vdi_4 WINXP. vdi_54 WINXP. vdi_69 WINXP. vdi_83 WINXP. vdi_98

WINXP. vdi_11 WINXP. vdi_26 WINXP. vdi_40 WINXP. vdi_55 WINXP. vdi_7 WINXP. vdi_84 WINXP. vdi_99

WINXP. vdi_12 WINXP. vdi_27 WINXP. vdi_41 WINXP. vdi_56 WINXP. vdi_70 WINXP. vdi_85 xen-Images

The first hard connection is successfully created. Run the DF command to view the usage of the partition space. If the file is copied, the system reports an exception due to insufficient disk space, regardless of the speed.

The above is a hard connection. A soft connection is also called a symbolic link. It is an independent file that points to a new inode, but it is recognized as a symbolic link when operating on it, thus, all operations are applied to the inode to which the object points. For example:
[WHB @ jcwkyl test] $ ln-s testfile symboliclink

[WHB @ jcwkyl test] $ stat symboliclink

File: 'symboliclink'-> 'testfile'

Size: 8 blocks: 0 Io block: 4096 Symbolic Link

Device: 802 H/2050d inode: 9306117 links: 1

Access: (0777/lrwxrwxrwx) uid: (500/WHB) gid: (500/WHB)

Access: 16:45:14. 000000000 + 0800

Modify: 16:45:09. 000000000 + 0800

Change: 16:45:09. 000000000 + 0800

As you can see, its file type is symbolic link, its inode number is 9306117 (the inode Number of testfile and hardlink is 9306116), and its links is 1. The soft connection does not affect the reference count of an inode.

Summary:
A soft connection is equivalent to a shortcut.
You cannot create hard connections for directories, but you can create soft connections for directories.
You cannot create hard connections across file systems or partitions, but you can create soft connections.

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.