Learning notes: Linux hard Links

Source: Internet
Author: User

1. Link concept:

In the Linux system, the links can be divided into two kinds: one for hard links and the other for links or symbols (symbolic link or soft link).

Before we talked about LN, the command was to create a link file, and in the case of no parameters, the link created by the LN command was a hard link

If you use the Ln-s create link, the front file type L (l) is a software link

In fact, the practices command demonstrates understanding:

Hard Link: ln source file ----> through the index nodes (inode).

Software Link: ln-s source file (the target file cannot be pre-existing)


1.1 Hard Link:

1) through the index nodes (inode) to enter the link

2) The hard link file will be a different entrance to the file

3) allows a file to have multiple valid path names (multiple portals)

4) You can avoid files being misunderstood


Demonstrate:

[[Email protected] oldboy]# pwd/oldboy[[email protected] oldboy]# echo 1  > a[[email protected] oldboy]# ls -l a-rw-r--r--.  1 root  root 2 jul 17 22:40 a[[email protected] oldboy]# cat a1[[ email protected] oldboy]# ln a b[[email protected] oldboy]# ls - hil a b -----> Note   The inode of the two files is the same 390210 -rw-r--r--.  2 root root 2  jul 17 22:40 a   390210 -rw-r--r--.  2 root root 2  Jul 17 22:40 b[[email protected] oldboy]# rm -rf a[[email  Protected] oldboy]# ls -lhi a b ls: cannot access a: no  such file or directory390210 -rw-r--r--.  1 root root 2 jul  17 22:40 b[[Email protected] oldboy]# cat b1 


Because the principle of the file system (EXT2) is that, as long as the index of the file (Inode index) has more than one hard link, only one of them is erased.

Hard links (that is, simply deleting the link point of the file) do not affect the index nodes themselves and the others (that is, the file is not erased), only

When the last link to a file is deleted, this time if there is a new one stored on the hard disc or when the system passes a hard check like fsck.

The numbers of deleted files and the links of the records are released, and the space is occupied and covered by new figures. At this time, the numbers can no longer be retrieved. That is to say,

In the Linux system, deleting a static file (without an application) (the document is also a file) has all the hard link files associated with it removed. (This can be understood at a later time, and will also be related to the occupation of the process)



Creating a hard Link:

Directly execute the command "ln source file Hard link file", you can complete the creation of hard link, the case of the following demonstration.

Hard Link Knowledge Summary:

1) Multiple files with the same inode number as hard link files

2) Delete the hard link file or delete the source file any one, the file is not erased

3) The file will be erased only if the source file and all the corresponding hard link files are deleted

4) When all the hard link files and the source files are erased, and then the new numbers are used to occupy the space of this file, or when the magnetic disc fsck is checked, the erased numbers are also collected by the system.

5) A hard link file is another entrance to the file (as in the front door of the supermarket and the back door)

6) can prevent important files from being deleted by setting hard link files to file

7) through the Execute command "ln source file Hard link file", you can complete the creative link

8) The hard link file is a normal file, so you can delete it with the RM command

9) for a static file (without the file being adjusted), the file is erased when the response is 0 (i_link). I_link's Way of viewing is (the third column of Ls-l's results)



1.2 Software link

The Soft link is also known as the symbol chain link (symbolic Links). The software link files in Linux are similar to shortcuts in Windows systems. The software link file in Linux is actually a special file,

The file type is L. The software link file can actually be understood as a text file, this text file contains the location information of the software link pointing to another source file, so the access to this "shortcut file" can quickly locate

The source file that the software links to.


Demonstrate:

[[email protected] oldboy]# Touch File[[email protected] oldboy]# ln-s file Soft_file[[email protected] oldboy]# Ls-lhi File Soft_file----> two have different 390210-rw-r--r--. 1 root root 0 Jul 23:33 file390227 lrwxrwxrwx. 1 root root 4 Jul 23:34 soft_file-File


The creation of software links:

Execute the command "ln-s source file Software link file" to complete the creation of the link, the case of the following demonstration

Misunderstanding area: the creation of a software link source file is required to exist, to create a software link file can not exist, is to be created with the LN command.

Links to Knowledge:

1) Software link similar to Windows shortcuts (can be viewed through readlink to see its point)

2) software links similar to a text file, which is stored in the source file path, pointing to the source file.

3) Delete the source file, the software link file still exists, but no access to the source file path content.

4) When it fails, it is a flashing hint of white-lettering red bottom. Oldboy_soft_link_file-Oldboy_file

5) Execute the command "ln-s source file Software link file" to complete the creation of the link (the target cannot exist)

6) Software links and source files are different types of files, but also different files. The inode number is also different.

7) The file type of the software link (l) can be used with the RM command.



CI Show:

[[email protected] oldboy]# mkdir/test/oldboy-p[[email protected] oldboy]# Cd/test/oldboy/[[email protected] oldboy]# t Ouch Oldboyfile[[email protected] oldboy]# ln oldboyfile oldboy_hard_link[[email protected] oldboy]# ln-s oldboyfile oldb Oyfile_soft_link[[email protected] oldboy]# Ls-lhi----> Note the difference between the inode and the same total 0402520-rw-r--r--. 2 root root 0 Jul 22:25 oldboyfile402521 lrwxrwxrwx. 1 root root 22:26 oldboyfile_soft_link-Oldboyfile402520-rw-r--r--. 2 root root 0 Jul 22:25 oldboy_hard_link



1.3. Links to related documents:

Through these tests, we can get a couple of conclusions:

1) Remove the software link oldboyfile_soft_link, Oldboyfile, Oldboy_hard_link no impact.

Global discussion: Deleting a software link file has no effect on source files and hard link files.

2) Remove hard link oldboy_hard_link, Oldboyfile, Oldboyfile_soft_link have no impact.

Global discussion: Deleting a hard link file does not affect the source file and the software link file.

3) Delete the original file Oldboyfile, the Oldboy_hard_link does not affect, but will cause oldboyfile_soft_link failure

Global discussion: Delete the source file, does not affect the hard link file, but will cause the software link invalidation, white-letter red bottom blink flashing.

4) Delete the source file Oldboyfile and Hard link oldboy_hard_link at the same time, it will cause the file to be erased.

5) The snapshot function in many hardware facilities is the use of the hard link principle.

6) The source file and the hard link file have the same inode number, which can be regarded as multiple portals of the same file or file

7) The source file and the software link file Inode is different, is a different file, the software link file as a shortcut to the source file, containing the source file location point.



1.4 A link to the catalogue:

1) It is not possible to create a hard link, but you can create a link

2) The software link to the catalog is a common skill in the production landscape (example: The second class before the Apache Enterprise)

3) The hard link of the catalogue cannot be cross the file system (can be understood from the hard link principle)

4) There is a hard link in each of the items below "." and a hard link to the top of the category "..."

5) Create a subdirectory in the parent's record, and the link number for the parent is increased by 1 (all the items are in the subdirectory). Point to Parent)

But creating files in the parent's records will not increase the number of links that the parent is recording



Example Demo:

[[Email protected] oldboy]# mkdir oldboydir[[email protected] oldboy]# ln  oldboydir oldboydir_hard_linkln:  ' Oldboydir ': hard link not allowed  for directory     ----> Cannot create hard links [[Email protected] oldboy]# ln  -s oldboydir oldboydir_soft_link[[email protected] oldboy]# ls -lhitotal  4.0K402522 drwxr-xr-x. 2 root root 4.0K Jul 18 23:05  oldboydir402520 lrwxrwxrwx. 1 root root    9 jul 18  23:06 oldboydir_soft_link -> oldboydir[[email protected] oldboy]# ls -a  oldboydir oldboydir/. -ldi          ----> Note that the inode is the same  402522 drwxr-xr-x. 2 root root 4096 jul 18 23:05  oldboydir402522 drwxr-xr-x. 2 root root 4096 jul 18 23:05 oldboydir/. [[email protected] oldboy]# ls -a oldboydir/]. &NBSP, .... /oldboy/ -ldi ----> Note The inode is the same 402519 drwxr-xr-x. 3 root root 4096  JUL 18 23:06&NBSP, .... /oldboy/     (itself, below the point, below, in the catalogue). Also a) 402519 drwxr-xr-x. 3 root root 4096 jul 18 23:06 oldboydir/.

1.5 Enterprise face:

Enterprise Face 10: Describe Linux software links and hard link areas

1) without reference, the LN command creates a hard link, and the ln command with the-s-parameters creates a software link

2) The Software link file is the same as the inode of the source file, and the inode of the software link file differs from the source file

3) The LN command cannot create a hard link to a record, but can create a link that is often used in a software link.

4) Delete the software link file, the source files and hard link files do not affect

5) Delete the hard link of the file, do not affect the source file and the software link file

6) Delete the source file of the link file, the hard link file does not affect, but will cause the software link file invalidation

7) Delete the source file and its hard link files at the same time, and the whole file will be completely erased.

8) A lot of hardware in the snapshot function, the use of similar hard link principle

9) Software link files can cross file system, hard link files can not cross file system

The above-mentioned problems of the Enterprise 1:

1. Introduction to the concept of hard links

2. How to create a hard link

3. Hard links to documents

4. Hard link to the catalog

The way of the above-mentioned enterprises ' problems 2: Painting


Enterprise Case:

Deep parsing of Web server disk full failures

C.P http://oldboy.blog.51cto.com/2561410/612351

The actual operation please see the Enterprise Case (1)


This article is from my Linux Dreams blog, so be sure to keep this source http://12098022.blog.51cto.com/12088022/1950058

Learning notes: Linux hard 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.