Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

6.8 Hard and soft links


May 23, 2021 That's what Linux should learn



After leading you through all the hard disk management knowledge in this chapter, Mr. Liu can finally explain the "shortcuts" in Linux system with confidence and boldness. I n a Windows system, a shortcut is a linked file that points to the original file, allowing users to access the original file from a different location; However, this seemingly simple thing is not the same in Linux systems.

There are hard links and soft connections in Linux systems.

Hard link: You can think of it as a "pointer to the original file inode" for which the system does not assign separate inode and files. T herefore, the hard link file and the original file is actually the same file, but the name is different. F or every hard link we add, the number of inode connections for the file increases by 1, and it is only if the number of inode connections for the file is 0 that it is completely deleted. I n other words, because a hard link is actually a pointer to the original file, even if the original file is deleted, it can still be accessed through the hard link file. It is important to note that due to technical limitations, we cannot link directory files across partitions.

Soft links (also known as symbolic links): Contains only the path name of the linked file, so you can link directory files or link across file systems. However, when the original file is deleted, the linked file will also be invalidated, which is of the same nature as "shortcuts" in Windows systems.

ln command

The ln command is used to create a linked file in the format "ln (options) target" with the available parameters and effects as shown in Table 6-6. W hen using the ln command, adding the -s parameter creates two "shortcuts" of a different nature. Therefore, without solid theoretical knowledge and practical experience to pave the way, although the successful completion of the experiment, but never understand why the success.

The parameters and effects available in the Table 6-6 ln command

Parameter Role -s Creates "Symbolic Links" (creates hard links by default if you don't have -s parameters) -f Force the creation of a link to a file or directory -i Ask before overwriting -v Shows the process of creating links In order to better understand the different properties of soft links and hard links, next create a soft link similar to shortcuts in Windows systems. This way, when the original file is deleted, the newly created linked file cannot be read.

[root@linuxprobe ~]# echo "Welcome to linuxprobe.com" > readme.txt [root@linuxprobe ~]# ln -s readme.txt readit.txt [root@linuxprobe ~]# cat readme.txt Welcome to linuxprobe.com [root@linuxprobe ~]# cat readit.txt Welcome to linuxprobe.com [root@linuxprobe ~]# ls -l readme.txt -rw-r--r-- 1 root root 26 Jan 11 00:08 readme.txt [root@linuxprobe ~]# rm -f readme.txt [ r oot@linuxprobe. cat readit.txt cat: readit.txt: No such file or directory next creates a hard link for the original file, which is equivalent to creating a pointer for the hard disk storage location of the original file, so that the newly created hard link no longer depends on information such as the name of the original file and is not readable due to the deletion of the original file. You can also see that the number of hard drive links for the original file increased to 2 after the hard link was created.

[root@linuxprobe ~]# echo "Welcome to linuxprobe.com" > readme.txt [root@linuxprobe ~]# ln readme.txt readit.txt [root@linuxprobe ~]# cat readme.txt Welcome to linuxprobe.com [root@linuxprobe ~]# cat readit.txt Welcome to linuxprobe.com [root@linuxprobe ~]# ls -l readme.txt -rw-r--r-- 2 root root 26 Jan 11 00:13 readme.txt [root@linuxprobe ~]# rm -f readme.txt [ root@linuxprobe ~]# cat readit.txt Welcome to linuxprobe.com