Linux system hard links and soft links

Source: Internet
Author: User

Linux system Hard links and soft links


"Concept of linking"

in the There are two types of links in a Linux system: one for hard linksand another for soft links or symbolic links (symbolic link or soft link). Ln This command is the command to create a linked file, which, by default, is a hard link created without parameters.

"Soft link"

Soft links are also known as symbolic links ( Symboliclink). Soft-link files in Linux are similar to shortcuts in Windows systems. The soft-link file in Linux is actually a special file. In a soft link, a soft link file is actually a text file that contains the information content of a soft link pointing to another source file location, so you can quickly navigate to the source file entity that the soft link points to by accessing the shortcut.

Summarize:

1, soft links similar to Windows shortcuts, soft links inside the source file is stored in the path, and the execution of the source file.

2 . Delete the source file, the soft link file still exists, but cannot access the content of the path pointed to.

3, execute the command "ln–s source file Soft link File", you can complete the creation of soft links.

4, soft links and source files are different types of files, but also different files. different Inode numbers

5. rm–f Soft link file

Example:

using ln–s to create a soft link, a soft link equivalent to a shortcut, can be based on a file or directory-based

[[email protected] zdw]# ln-s/application/apache2.2.31//application/apache Create a soft link file

[[email protected] zdw]# ll/application/apache View Soft link file readlinkapache can also

lrwxrwxrwx. 1 root root 10:27/application/apache-/application/apache2.2.31/

[Email protected] zdw]# TOUCH/APPLICATION/APACHE2.2.31/ZDW

[Email protected] zdw]# ll/application/apache/

Total 0

-rw-r--r--. 1 root root 0 Mar 10:27 zdw

[Email protected] zdw]# ll/application/

Total 8

lrwxrwxrwx. 1 root root 10:27 apache->/application/apache2.2.31/

Drwxr-xr-x. 2 root root 4096 Mar 10:27apache2.2.31

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7D/2A/wKiom1bhHlqDgu79AANHA2nr84c088.jpg "title=" 3.jpg " Width= "730" height= "208" border= "0" hspace= "0" vspace= "0" style= "width:730px;height:208px;" alt= " Wkiom1bhhlqdgu79aanha2nr84c088.jpg "/>

The default link is a light blue color, if the link fails to be red flashing

"Hard Link"

a hard link is defined by an index node ( Inode) to link to. in the Linux (EXT2,EXT3) file system, files saved in a disk partition will be assigned a number regardless of the type, which is called the index node number (Inode index), or inode , which is the number of the file in the system.

in the In a Linux file system, it is normal and permissible for multiple filenames to point to the same index node (Inode) . Files in this case are called hard links. One of the functions of a hard link is to allow a file to have multiple valid paths (multiple portals) so that users can establish hard links to important files to prevent "accidental deletion" of source data (many hardware storage, such as the snapshot feature in NetApp storage, should be the same principle, Add a snapshot to one more hard link). Why does a file create a hard link that prevents the data from being mistakenly deleted?

because the file system (ext2) the principle is that as long as the file's index node ( Inode Index) There is also one or more hard links. Deleting only one of the links (that is, simply deleting the link to the file) does not affect the index node itself and other links ( that is, the data file ), only if the last link to the file is deleted, if new data is to be stored on the hard disk, or if the system passes a similar When fsck does a disk check. A link to a deleted file's data block and directory is released, and the space is occupied and overwritten by the data. At this point, the data can no longer be retrieved. In other words, in a Linux system, deleting a file (the directory is also a file) is conditional on all the hard-linked files that are associated with it being deleted. Tip: A hard link is the equivalent of another entry in a file.

Summarize:

1. A hard-link file is a different file name with the same inode node number.

2, Delete the hard link file or delete one of the source files, the file entity and the end is deleted.

3, only delete the source files and all the corresponding hard connection files, the file entity will be deleted.

4, when all the hard link files and source files are deleted, and then the new data will occupy the space of this file, or the disk will do the fsck check, the data will be recycled.

5 . Hard link file is another entrance to the file.

6 . You can prevent files from being mistakenly deleted by setting hard-link files to files.

7, can execute the command "ln source file Hard link file" can be done to create a hard link.

8. rm–f Delete hard-link files

Example:

use ln to create a hard link. You can see that the number of nodes is the same

[Email protected] zdw]# LN zdw.txt a

[[email protected] zdw]# ln a b

[Email protected] zdw]# Ls-li

Total 16

131074 Drwxr-xr-x. 2 root root 4096 Mar 1010:39 123

131076-rw-r--r--. 3 root root 9 17:40 a

131076-rw-r--r--. 3 root root 9 17:40 b

131076-rw-r--r--. 3 root root 9 17:40 zdw.txt

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7D/28/wKioL1bhHyPSnd8-AAGY-TVfaPw841.jpg "title=" 4.jpg " Width= "730" height= "147" border= "0" hspace= "0" vspace= "0" style= "WIDTH:730PX;HEIGHT:147PX;" alt= " Wkiol1bhhypsnd8-aagy-tvfapw841.jpg "/>

So the source file is deleted, the data is not lost

[[email protected] zdw]# echo 123 >zdw.txt Insert 123 in text

[Email protected] zdw]# cat Zdw.txt

123

[[email protected] zdw]# rm-f zdw.txt Delete source file

[Email protected] zdw]# Cat a

123

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M02/7D/28/wKioL1bhH0bxWuDLAADWhD6O2Gg541.jpg "title=" 5.jpg " alt= "Wkiol1bhh0bxwudlaadwhd6o2gg541.jpg"/>

Two together to make a file presentation

As you can see, the Hard link node number is the same 1835012, the link number hard link is 2, the soft link is 1

[Email protected] zdw]# mkdir/test/zdw-p

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

[Email protected] zdw]# touch zdwfile

[Email protected] zdw]# LN zdwfilezdw_file_hard_link

[Email protected] zdw]# ln-s zdwfilezdw_file_soft_link

[Email protected] zdw]# Ls-li

Total 0

1835012-rw-r--r--. 2 root root 0 Mar 1014:25 zdwfile

1835012-rw-r--r--. 2 root root 0 Mar 1014:25 zdw_file_hard_link

1835013 lrwxrwxrwx. 1 root root 7 Mar 1014:26 Zdw_file_soft_link-Zdwfile

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7D/2A/wKiom1bhHvezUD_4AAJXEZc6KAM035.jpg "title=" 6.jpg " Width= "730" height= "245" border= "0" hspace= "0" vspace= "0" style= "width:730px;height:245px;" alt= "Wkiom1bhhvezud_ 4aajxezc6kam035.jpg "/>

The following example shows that a directory can create a soft link, but cannot create a hard link, and for a directory soft link, the actual factory view is often used.

[Email protected] zdw]# mkdir zdwdir

[Email protected] zdw]# LN zdwdirzdw_dir_hard_link

ln: ' zdwdir ': Hard link not allowed Fordirectory

# directory cannot create hard links, hard links require the same partition, different partitions, even if the node number is the same, is not a thing

[Email protected] zdw]# ln-s zdwdirzdw_dir_soft_link

[Email protected] zdw]# Ls-li

Total 16

131077 Drwxr-xr-x. 2 root root 4096 Mar 1014:38 Zdwdir

131078 lrwxrwxrwx. 1 root root 6 Mar 14:39 zdw_dir_soft_link->zdwdir

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7D/2A/wKiom1bhHybRzIC1AALfpMCArYo992.jpg "title=" 7.jpg " Width= "730" height= "244" border= "0" hspace= "0" vspace= "0" style= "width:730px;height:244px;" alt= " Wkiom1bhhybrzic1aalfpmcaryo992.jpg "/>

the zdwdir node is 2 because the directory

.. Top level directory

. current directory

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7D/28/wKioL1bhH-WxSqE8AAEYIRR-BoA364.jpg "title=" 8.jpg " Width= "730" height= "126" border= "0" hspace= "0" vspace= "0" style= "width:730px;height:126px;" alt= " Wkiol1bhh-wxsqe8aaeyirr-boa364.jpg "/>

because the Zdwdir and its own directory are the same as the hard-link file, so the node number is the same, so the number of Zdwdir links is 2

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7D/28/wKioL1bhH_fQH9LHAAG07EhYaYA180.jpg "title=" 9.jpg " Width= "730" height= "171" border= "0" hspace= "0" vspace= "0" style= "width:730px;height:171px;" alt= "Wkiol1bhh_ Fqh9lhaag07ehyaya180.jpg "/>

similarly : is the previous level directory, and the ZDW node number is the same

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/7D/2A/wKiom1bhH4fxHK7vAAFwP4L-O48713.jpg "title=" 10.jpg "Width=" 730 "height=" 229 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:730PX;HEIGHT:229PX; "alt=" Wkiom1bhh4fxhk7vaafwp4l-o48713.jpg "/>


This article is from the "Zhao Dongwei Learning record" blog, so be sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1749593

Linux system hard links and soft links

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.