Deep understanding of Linux hard links and soft links

Source: Internet
Author: User

In the process of learning Linux, I often put soft hard links, so today specializes in a number of data, and then integrated in their own blog, easy to review later, view.

On the one hand also because saw Vamei Linux concept and System series articles, feel not complete to do some supplement.

Soft links correspond to shortcuts in Windows. Hard link is equivalent to a disaster preparedness system, data stored in two, and replication is different from the synchronization mechanism between the two, a change of data will be synchronized to another place, in addition, a data if deleted, will not affect the other data.

The detailed introduction:

A hard connection points to a node (inode), is another name for an existing file, modifies one, and the file it is connected to is modified simultaneously, and the effect is the same as the soft link when read and write to the hard linked file. However, if we delete the source file for the hard link file, the hard link file still exists and the original content is preserved. At this point, the system "forgot" it used to be a hard link file, and regard him as a common document. Hard link file has two restrictions: 1, not allowed to create a hard link to the directory; 2. Links can only be created between files in the same file system.

Soft links point to the path, also known as the symbolic link, this file contains the path name of another file can be any file or directory, but also linked to different file system files, and win under the same shortcut. Soft link files can even link nonexistent files, which produces what is commonly called a "broken chain" problem (or "phenomenon"), and linked files can even be looped around themselves, similar to recursion in a programming language.

Example:

ln Exitfile NewFile

Ln-s Exitfile NewFile

The newfile here is the name of the link created, the first without parameters, the default is hard link, the second-s is the soft link.

Ls-il can view attributes such as the inode number of a file, such as:

[Root@server tmp]# Touch file

[root@server tmp]# ln file file1

[Root@server tmp]# ln-s file file2

[Root@server tmp]# Ls-il

Total 0

27127-rw-r--r--. 2 root root 0 Nov 10:40 file

27127-rw-r--r--. 2 root root 0 Nov 10:40 file1

27132 lrwxrwxrwx. 1 root 4 Nov 10:40 file2-> file

In the/tmp directory, create files named file, successively create hard link file1, soft link file2. Observe the output of Ls-il

File and File1 's inode are the same, so it can be said that the hard link is the pointer to the same inode, the system does not reassign the inode for him. File2 points to file, indicating that the file2 is a soft link and the inode number has changed,

If you delete the original file, the hard link continues to be used, but the soft link fails. This is similar to the shortcut under win.

Here you need to emphasize that the ls-l command output in the 3rd field of content, this is the number of inode links, as long as not 0, for hard links, the file has always existed, whether you delete the source file or the previous generated 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.