Second, the Linux system hard links and soft links detailed introduction and practice

Source: Internet
Author: User

The concept of links

in Linux systems, links can be divided into two types: one called hard link, and the other is called soft link or symbolic link (symbolic link ). With the default without parameters, the link created by the Execute ln command is a hard link.

Hard Links

a hard link is a link through an index node. in a Linux 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 index .

in the In a Linux system, multiple file names pointing to the same index node (Inode) are normal and allowed. In general, such links are called Hard links. One of the functions of a hard link is to allow a file to have more than one valid pathname so that users can establish hard links to important files to prevent "accidental deletion" of source data (many hardware, such as the snapshot feature in NetApp storage, applies this principle, Add a snapshot with more hard links). Why does a hard link to a file prevent the data from being mistakenly deleted?

because the file system works as long as the file's index node ( Inode Index) There is more than one link (only the file point is deleted). Deleting only one of the links does not affect the index node itself and other connections (that is, the data entity is not deleted), only when the last link is deleted, if there is new data to be stored on the hard disk, the deleted file's data block and directory link will be freed, the space is overwritten by new data. At this point, the data can no longer be retrieved. That is, in a linux System, deleting a file (in fact a directory is also a file) is the condition that all the hard-linked files associated with it are deleted (this is understood for the time being, and then the process is occupied by the text). Tip: A hard link is the equivalent of another entry in a file.

Soft links

soft links are sometimes referred to as symbolic links ( Symbolic Link). Soft-link files are similar to shortcuts in Windows systems. It is actually a special file, in the soft link, the soft link file is actually a text file, this file contains a soft link to another file location information content, so by accessing this "shortcut" can quickly locate the soft link to point to the source file entity.

Sample Demo

File Demo

[Email protected] ~]# Mkdir/test/oldboy-P[[email protected]~]# cd/test/oldboy/[ email protected] oldboy]# touch oldboyfile #创建一个测试文件oldboyfile [[email protected] oldboy]# LN oldboyfile oldboyfile _hard_link #创建一个oldboyfile的一个硬链接文件oldboyfile_hard_link [[email protected] oldboy]# LN-s Oldboyfile oldboyfile_soft_link #创建一个oldboyfile的一个软链接文件oldboyfile_soft_link [[email protected] oldboy]# ls-Li #-I parameter to view inode node information for a file total A810403-rw-r--r--2Root root0May1 Geneva: +Oldboyfile810403-rw-r--r--2Root root0May1 Geneva: +Oldboyfile_hard_link810404lrwxrwxrwx1Root rootTenMay1 Geneva: -Oldboyfile_soft_linkOldboyfile

More clearly

Summary: From the above example results, you can see that the hard-link file oldboyfile_hard_link

The inode node number is the same as the source file oldboyfile, both 810403, but the soft-link file oldboyfile the inode node number of the _soft_link is different from the source file odboyfile , which is 810404 .

Catalog Example

[ email protected] oldboy]# mkdir oldboydir #创建一个测试文件oldboydir [[email protected] oldboy]# LN oldboyoldboydir/oldboyfile_hard_link oldboyfile oldboyfile_soft_link [[email protected] oldboy]# LN oldboydir OLDBOYDIR_HARD_LINKLN: ' Oldboydir': Hard link isn't allowed for directory #目录无法创建硬链接[Email protected] oldboy]# LN-s Oldboydir oldboydir_soft_link #但目录可以创建软链接 and is often used in production environments. [[email protected] oldboy]# ls-Lirttotal -810403-rw-r--r--2Root root0May1 Geneva: +Oldboyfile_hard_link810403-rw-r--r--2Root root0May1 Geneva: +Oldboyfile810404lrwxrwxrwx1Root rootTenMay1 Geneva: -Oldboyfile_soft_linkOldboyfile810405Drwxr-xr-x2Root root4096May1 Geneva: -Oldboydir810406lrwxrwxrwx1Root root9May1 Geneva: -Oldboydir_soft_linkoldboydir Hint: Red section for this example

More clearly

Summary:The ln command cannot create a hard link to a directory, where you can create a soft link to a catalog, and a soft link to a catalog is a common feature of production scenarios.

Delete Example

" This is oldboyfile " > >oldboyfile[[email Protected] oldboy]# cat oldboyfile  This is  This  is  This is-F oldboyfile   #删除原始文件 is actually a hard link to delete a file

Clearer:

 This  is oldboyfile Hint: Strange, the content is still in, actually this is because has the oldboyfile_hard_link hard link existence, therefore even if deletes the oldboyfile, but still has the hard link to the file Inode node, therefore, At this point the file will not be released and deleted, and when the file Oldboyfile is deleted, only the connection oldboyfile to the Inode node of the file is deleted. [email protected] oldboy]# cat Oldboyfile_soft_link cat:oldboyfile_soft_link:No such file or directory summary: Through the above test can be seen: when Delete original file oldboyfile, hard link oldboyfile_hard_link not affected, and oldboyfile_soft_link invalid. 

Link Summary

Related Documents

Through the above tests, the following conclusions can be obtained:

1 . Delete Soft link oldboyfile_soft_link, no impact on Oldboyfile,oldboyfile_hard_link; 2 Delete Hard link oldboyfile_hard_link, no effect on Oldboyfile,oldboyfile_soft_link; 3 Delete oldboyfile, no impact on hard link oldboyfile,oldboyfile_hard_link, resulting in soft link oldboyfile_soft_link invalidation.  4. Simultaneously delete the source file Oldboyfile, hard link oldboyfile_hard_link, the whole file will be really deleted.  5. Many of the snapshot functions already in the device are based on the principle of hard links.

about Catalogs

1. for a directory, you cannot create a hard link, but you can create a soft link

2. soft links to catalogs are a technique used in a production environment

Second, the Linux system hard links and soft links detailed introduction and practice

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.