Hard links and soft links in Linux

Source: Internet
Author: User
Tags parent directory

I. Overview of the LN command


Its function is to create a synchronized link for a file in another location. When we need to use the same file in different directories, we don't need to put a file in every required directory, we just put the file in a fixed directory, and then The other directory uses the ln command to link it so that it does not have to use disk space repeatedly.


Second, the difference between hard links and soft links


Hard link: Creates a new file name from the Inode connection of the file system, rather than generating a new file

Soft Link: A file similar to the Windows shortcut feature that allows you to quickly connect to a target file (or directory)


Third, hard links

Syntax:ln filename [linkname]


1. Creating a hard link will add additional record entries to reference the file

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

3. Each directory references the same inode number

4. Increment the number of links at creation time (the number of links is equivalent to a file with several names )

For example:

3 indicates the number of links, stating that A's file name has three a,b,c

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/85/3D/wKiom1ed6OnQ3SxoAAANRJwSeaw723.png "title=" 2.png " alt= "Wkiom1ed6onq3sxoaaanrjwseaw723.png"/>

5. When deleting files:

Link for RM command decrement count

File to exist, with at least one link number

When the number of links is zero, the file is deleted

For example:

After deleting a, the number of links in B is reduced by one, which becomes 2, when the name is B and C.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/3D/wKioL1ed6U_TpSyZAAAQ000llL8893.png "title=" 3.png " alt= "Wkiol1ed6u_tpsyzaaaq000lll8893.png"/>

6. Cannot cross drive or partition

A different directory in the same partition can create a hard link, but different partitions cannot create

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/3D/wKioL1ed6Yjgl1JDAADc0UT4CCs983.png "title=" 4.png " alt= "Wkiol1ed6yjgl1jdaadc0ut4ccs983.png"/>

7, the directory can not establish a hard link, but the directory node number will change

For example:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/3E/wKiom1ed6ijQecpmAAAGmNbMH2g411.png "title=" 5.png " alt= "Wkiom1ed6ijqecpmaaagmnbmh2g411.png"/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/3E/wKiom1ed6jWhAhO4AAAJ08N3nzQ324.png "title=" 6.png " alt= "Wkiom1ed6jwhaho4aaaj08n3nzq324.png"/>

because. Represents the current directory, so the/bin node number is 2

Create a folder with a node count of one, because there is a parent directory under the folder " . "    

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/85/3D/wKioL1ed6vHgtAcsAAAMD4zGsCc038.png "title=" 11.png "Style=" Float:none; "alt=" Wkiol1ed6vhgtacsaaamd4zgscc038.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/3E/wKiom1ed6vGAOJ0oAAAGIfY1po4128.png "title=" 12.png "Style=" Float:none; "alt=" Wkiom1ed6vgaoj0oaaagify1po4128.png "/>

It is important to note that:

In CENTOS6, the number of nodes is 2 independent partition, the number of nodes is 1 is the virtual directory

In Centos7 if the root of the partition is as far as 128


Four, soft link

Syntax: ln-s filename [linkname]


1. A symbolic link pointing to another file

2. LS-L displays the name of the link and the referenced file

3, the content of a symbolic link is the name of the file it refers to

4, the directory can be

For example:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/3D/wKioL1ed7DnxhbZoAAAEbLYc89w418.png "title=" 13.png "Style=" Float:none; "alt=" Wkiol1ed7dnxhbzoaaaeblyc89w418.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/3E/wKiom1ed7Dmx_hoBAAAL6SByqZk814.png "title=" 14.png "Style=" Float:none; "alt=" Wkiom1ed7dmx_hobaaal6sbyqzk814.png "/>

5, can cross the partition

A soft connection does not specify a relative path or an absolute path when an error occurs, they will think that your link file and your source files are in the same directory, in a soft connection is generally used relative path

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/85/3E/wKioL1ed7MWwrkEeAAAM0fk-Ljg976.png "title=" 15.png "alt=" Wkiol1ed7mwwrkeeaaam0fk-ljg976.png "/>

6. Ls–s Original file soft link

Original file relative path: relative to the path of the soft link file, not to the current working directory

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/3E/wKiom1ed7VDwCIFYAAAFNKfLRwo509.png "title=" 16.png "Style=" Float:none; "alt=" Wkiom1ed7vdwcifyaaafnkflrwo509.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/3E/wKioL1ed7VDQFFzhAAAQVq9M6SI861.png "title=" 17.png "Style=" Float:none; "alt=" Wkiol1ed7vdqffzhaaaqvq9m6si861.png "/>

7, pointing to the path of another file, its size is the length of the path string pointed to, does not increase or decrease the target file Inode reference count;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/3E/wKioL1ed7bKADo3dAAAWQehVV70330.png "title=" 18.png "alt=" Wkiol1ed7bkado3daaawqehvv70330.png "/>


V. Summary

If you use ln without any arguments, it is hard link. And if you use the-s parameter, it is almost a shortcut under Windows meaning, when you modify the Linux symbolic link file, the change is actually "source files", so no matter where your source files are connected to, as long as you modify the connection file, The source file is changed.



Hard links and soft 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.