Analysis of hard links and symbolic links in Linux

Source: Internet
Author: User
Hard link and symbolic link (soft link): One of the most important features of a Linux file system is its File link. The link is a reference to the file, so that you can see the file in the file system. The file stored in the disk partition is assigned a number no matter what type it is stored in... Info & nbsp;
Hard link and symbolic link (soft link ):
One of the most important features of a Linux file system is its File link. The link is a reference to the file, so that you can see the file in the file system. The file stored in the disk partition is assigned a number no matter what type it is, called the Index node number inode. In Linux, the link can be the same as the original file. Links can be executed, edited, and accessed like normal files. For other applications in the system, the link is the original file corresponding to it. When you edit a file through a link, you actually edit the original file. The link is not a copy. There are two types of links: hard link and symbolic link (soft link ). Www.2cto.com
Hard links can only reference files in the same file system. It references the physical indexes (also known as inode) of files in the file system ). When you move or delete the original file, the hard link will not be broken because it references the physical data of the file rather than the position of the file in the file structure. A hard-linked file does not require the user to have the permission to access the original file or display the location of the original file. this helps the file security. If the deleted file has a hard link, the file will still be retained until all references to it are deleted.
A soft connection is actually to create a new file, which is specifically used to point to other files (that is, it is very similar to the file in the windows shortcut ). A soft connection generates a new file, but the file is used to point to a specific file. if you delete this soft connection file, the connection is not required, it has nothing to do with the original object, but deleting the original object will make the corresponding soft connection unavailable (cat soft link file, the system prompts "this file or directory is not").
Differences between hard links and soft links;
Hard connections do not establish inode. they only increase by 1 in the original inode link count field of the file. therefore, hard links cannot span the file system. On the contrary, a soft connection will re-establish an inode. of course, the inode structure is different from other ones. it is just a string that specifies the source file. Once the source file is deleted, the soft connection becomes meaningless. When a hard link is deleted, the system will check the value of inode link count. if the value is greater than or equal to 1, inode will not be recycled. Therefore, the file content will not be deleted.
A hard link actually creates an alias for a file. the linked file and the original file are actually the same file. You can use ls-I to check whether the inode numbers of the two files are the same, indicating that they are the same file. the soft link creates a point, that is, the content in the linked file is a pointer to the original file. They are two files. Www.2cto.com
Soft links can be used across file systems, but hard links cannot be used. soft links can be used to link a non-existent file name (filename, linux automatically creates a file named filename. hard links are not allowed (files must exist and inode must exist). soft links can be used to connect directories, but hard links cannot. Both links can be created using the ln command. By default, ln creates hard links. You can use the-s switch to create a soft link.
Now let's look at the creation and nature of hard links and symbolic links.
You can use the cp and ln commands to create hard links and symbolic links.
Create a hard link and its nature: [plain] [guo @ guo ~] $ Cp-l link. back [guo @ guo ~] $ Ln link. back1 [guo @ guo ~] $ Ls-al | grep 'link * '-rw-r -- 3 guo 38 20:24 link-rw-r -- 3 guo 38 April 8 20:24 link. back-rw-r -- 1 guo 38 April 8 20:26 link. back1
The size of the created hard-link file is the same as that of the original file. the file attribute is normal.
When you modify any file, the content of the three files can be modified at the same time. [Plain] [guo @ guo ~] $ Cat link shell [guo @ guo ~] $ Cat link. back * shell [guo @ guo ~] $ Cat link shell [guo @ guo ~] $ Cat link. back shell [guo @ guo ~] $ Cat link. back1 shell [guo @ guo ~] $ Echo "shell"> link. back [guo @ guo ~] $ Cat link. back shell [guo @ guo ~] $ Cat link. back1 shell [guo @ guo ~] $ Cat link shell when deleting the original file, the hard link file still exists and the content remains unchanged. [Plain] [guo @ guo ~] $ Cat link shell [guo @ guo ~] $ Cat link. back shell [guo @ guo ~] $ Rm link rm: Do you want to delete a common file "link "? Y [guo @ guo ~] $ Cat link. back1 shell [guo @ guo ~] $ Cat link. back shell common users can create hard links for superuser files. [Plain] [guo @ guo ~] $ Ls-al | grep 'file2'-rw-r -- 2 root 89 Aug 17 21:51 file2 [guo @ guo ~] $ Cp-l file2 file2.back [guo @ guo ~] $ Ln file2 file2.back1 [guo @ guo ~] $ Ls-al | grep 'file2 * '-rw-r -- 4 root 89 April 6 21:51 file2-rw-r -- 4 root 89 April 6 21:51 file2.back-rw -r -- 4 root 89 April 6 21:51 file2.back1, but because the original file is a root user and has no write permission on other users, therefore, other users cannot modify the original file or hard-link file, but can delete the linked file. [Plain] [guo @ guo ~] $ Rm file2.back rm: Do you want to delete a normal file "file2.back" with write protection "? Y [guo @ guo ~] $ Rm file2.back1 www.2cto.com rm: Do you want to delete the write-protected normal file "file2.back1 "? Y from the above we can see that the link file write is protected. Create a symbolic link and its nature: create a symbolic link: [plain] [guo @ guo ~] $ Cp-s link. back [guo @ guo ~] $ Ln-s link. back1 [guo @ guo ~] $ Ls-al | grep 'link * '-rw-r -- 2 guo 12 April 8 20:37 link lrwxrwxrwx 1 guo 4 April 8 20:51 link. back-> link lrwxrwxrwx 1 guo 4 April 8 20:51 link. back1-> link note: The link file property is l, and the soft file size is different from the original file size. Like a hard link, the content of any file can be changed at the same time when three files are modified. this is not an example here. If a soft link deletes the original file, the linked file cannot be opened again. [Plain] [guo @ guo ~] $ Cat link shell [guo @ guo ~] $ Cat link. back shell www.2cto.com [guo @ guo ~] $ Rm link rm: Do you want to delete a common file "link "? Y [guo @ guo ~] $ Ls-al | grep 'link. back * 'lrwxrwxrwx 1 guo 4 April 8 21:02 link. back-> link lrwxrwxrwx 1 guo 4 April 8 21:02 link. back1-> link [guo @ guo ~] $ Cat link. back cat: link. back: no file or directory [guo @ guo ~] $ Cat link. back1 cat: link. back1. You can create directory links through soft links and link them across file systems. Author junjieguo
Related Article

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.