Differences between linux soft links and hard links

Source: Internet
Author: User

Introduction:

Link, a method for establishing 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 ).

Inode is an index node that stores the basic information of files and directories, including the time, file name, user, and group.

Block: The place where the file content and data are stored.

 

1. Command:

Hard: ln Source File link name

Soft: ln-s Source File link name

 

2. Principles:

Hard link:

Find two different file names through the block specified by inode of A or B, and read the final data regardless of the file name.

Each time a file name is added, the number of links on the inode node is added. Each time a corresponding file name is deleted, the number of links on the inode node is reduced by one until it is 0, inode nodes and corresponding data blocks are recycled.

Note: rm A only deletes the file name A. The data block (file) corresponding to A is recycled by the system only when the inode node connection count is reduced to 0.

Hard chain connection .png

 

Soft link:

Inode of A reads the content file name of the two files, links to the correct directory based on the file name to obtain the inode of the target file, and finally accesses the correct data. If the target file is deleted, data cannot be read through the link file.

Soft chain connection .png

3. Restrictions:

Hard Link: Hard Link)

A. Creating a hard link adds additional record items to reference files.

B. Corresponds to a physical file on the same file system

C. Each directory references the same inode number

D. Increase the number of connections during creation

E. Cannot cross-Filesystem

F. Cannot link the Directory

Note: When deleting an object:

Rm command to decrease the count link

The file must exist. There must be at least one link.

The file is deleted when the number of links is zero.

Soft Link: Symbolic Link (Symbolic Link, shortcut)

A. The content of a symbolic link is the name of the referenced file.

B. You can perform Directory

C. Cross-partition

D. Point to the path of another file; its size is the length of the path string to point to; does not increase or decrease the inode reference count of the target file

Differences between linux soft links and hard links

 

Hard link: When a new name is added to the file

 

 

When a link is added, the number of links is increased, and the inode node count is not increased.

 

 

Support Absolute and relative paths

 

 

Hard links cannot be created for directories.

 

You cannot create links across partitions.

 

When a hard link is linked to a file, you can still use the linked file to access the data of the deleted file.

 

 

Soft link: supports creating links to directories.

 

(Note: When you use rm-rf to delete a directory, the directory will not be deleted, but the content in the directory will be deleted and the source file will be deleted)

 

 

When a soft link is linked to a file, it is equivalent to creating a new file, and the inode node will increase

 

When a file linked to by a soft link is deleted, you cannot use the linked file to access the deleted data.

 

Soft links support cross-partition links

 

 

Soft links support absolute and relative paths, but note that they are not relative to the current working directory.

 

 

In some cases, it is better to use the relative path for soft links: for example, when backing up and moving directories

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.