0X00 What is a linked file
The linked file in Linux is the equivalent of a shortcut in Windows, which can be accessed by linking files to the source files that the link points to. However, there is a certain difference between the link file under Linux and the shortcuts in Windows. There are two types of link files in Linux hard links and soft connections also known as symbolic links
Before introducing the linked file, let's introduce the inode in the Linux file system.
An inode is a unique locator for files in a Linux file system, each of which has an inode and is unique, and each inode uniquely corresponds to one file.
Also familiar with the problem of pointers, if you have learned C + +, it will be easy to understand
All the data in the computer is stored in the disk, the use of the time will be read into memory, and disk and memory are a piece of space, we use the address to find the specific location of the data. An object (usually a variable) that holds a file or a data store address is called a pointer.
The files we see in the Linux operating interface are pointers that point to a specific location on the disk. So each time we click on the file, the system will be able to open the disk corresponding to the location of the files we need.
0X01 Hard Links
A hard link is a pointer to a specific location on the disk. If we create a file, we will allocate a location on the disk to store the file, and create a pointer to make it easier for us to find the file. And then we create a hard link to this file, is equivalent to creating a pointer to a location on the disk where the file resides, so even if we delete the file we created earlier (which is actually a pointer), the file itself will not be deleted because there is still a pointer to the file storage location. So we can still access the original file through the hard links we created later.
0X02 Soft Link--Symbolic link
A soft connection can be roughly understood as a pointer to a pointer. If we create a file and create a soft connection to the file, if we delete the original file, then the file is really not found. Because a soft connection is just a pointer to a previous point (pointing to a specific file location), the soft connection points to a blank area, and the previously existing files are not found. It's as if we uninstalled a software in Windows, but the shortcuts on the desktop might still be there, and now we don't have access to the software because we've deleted it.
0x03 Operation Demo
First I have a hehe.tar.gz file here, and we look at its inode is 33515290. Then create a hard link named heihei.tar.gz, and then look at the inode of the two files, found that the inode of two files is the same. Then create a soft connection named haha.tar.gz to continue viewing the file inode. You can see that the system assigns a new inode to the haha.tar.gz, and there is an arrow in LS that shows what files to point to.
Ls-i can display the inode of a file
0X04 Soft Hard link comparison
Hard Links:
Do not assign a new inode
You cannot link between different file systems
Only root can create a hard link to a directory
Can only originate from existing Files
Soft Link:
Assigning a new Inode
Not subject to file system restrictions
Identification pointing to the location of the source file
Can link to a nonexistent file