Soft connections and hard links under Linux

Source: Internet
Author: User

Because of the teaching reasons, need to download Android source code, later when using repo, the system prompts need to python2, my system is two, but the default is Python3, unable to download, through the creation of a link to do, the following is the link under Linux.

1.Linux Link Concept
There are two kinds of links under Linux, one is called hard link, and the other is called Symbolic link (symbolic). By default, the LN command produces a hard link by using the-s option, which produces a symbolic link, which is a soft link. A hard connection is a connection that is made through an index node. In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index). In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. That is, the condition that the file is actually deleted is that all the hard connection files associated with it are deleted. Symbolic connection (symbolic link), also known as soft connection. A soft-link file has a shortcut similar to Windows. It's actually a special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

2. Deepen understanding through experimentation
[email protected] linuxcode]$ Touch file1.txt #创建一个测试文件file1. txt
[Email protected] linuxcode]$ ln file1.txt file2.txt #创建f1的一个硬连接文件file2. txt
[Email protected] linuxcode]$ ln-s file1.txt file3.txt #创建f1的一个符号连接文件file3. txt
[[email protected] linuxcode]$ Ls-li #-I parameter displays inode node information for a file
Total dosage 0
11688147-rw-r--r--2 munication munication 0 May 8 05:26 file1.txt
11688147-rw-r--r--2 munication munication 0 May 8 05:26 file2.txt
11688148 lrwxrwxrwx 1 munication munication 9 May 8 05:27 file3.txt-file1.txt

As can be seen from the above results, the hard connection file F2 the same as the Inode node of the original file F1, all 11688147, but the inode node 11688148 of the symbolic connection file is obviously different.

[[email protected] linuxcode]$ echo "i m file1.txt" >> file1.txt #向file1. txt file write content I ' m file1.txt
[email protected] linuxcode]$ cat file1.txt #查看file1. txt content
I ' m file1.txt
[email protected] linuxcode]$ cat file2.txt #查看file2. txt content
I ' m file1.txt
[email protected] linuxcode]$ cat file3.txt #查看file3. txt content
I ' m file1.txt
[Email protected] linuxcode]$ rm-f file1.txt #删除file1. txt file
[[email protected] linuxcode]$ cat file2.txt #查看file2. txt content, files are still alive
I ' m file1.txt
[email protected] linuxcode]$ cat file3.txt #查看file3. txt content, the file has vanished, no longer exists
Cat:file3.txt: No file or directory

As you can see from the above test, hard connection File2.txt is not affected when the original file file1.txt is deleted, but the symbolic connection File3.txt file is not valid

Through the above practice, it is easy to draw the following conclusions:
1). Remove the symbolic connection file3.txt, the file1.txt,file2.txt will not have an impact;
2). Delete the hard connection File2.txt, the File1.txt,file3.txt also has no effect;
3). Delete the original file file1.txt, the hard connection file2.txt has no effect, will cause the symbolic connection file3.txt invalid;
4). Delete the original file file1.txt and file1.txt hard connection file2.txt, the entire file will be really deleted.

Soft connections and hard links under Linux

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.