1 Overview
Both hard and soft links are files.
2 hard links
Definition:
Each file is referenced by its inode number. The file name is just intended to improve user experience. Because of the separation of the file name and inode number (the file name is saved in the directory, rather than in the inode node), it is possible to direct multiple file names to the same inode number. All files must be hard-linked at least once and more than once.
Features:
1) The link count is positively related to the number of connected files. When the link count decreases to 0, the file will be deleted.
2) The attached hard link is a regular file, but it only references the inode Number of the host file. The rest are generally the same as those of the host file.
Syntax:
Ln file name Link name
Note: The Link name is not available beforehand.
Case:
[Root @ www.bkjia.com water] # touch w
[Root @ www.bkjia.com water] # cat> w
My name is www.bkjia.com
[Root @ www.bkjia.com water] # ll-I w
3496568-rw-r -- 1 root 17 09-25 14:48 w
[Root @ www.bkjia.com water] # ln w w_hardlink
[Root @ www.bkjia.com water] # ll-I
Total 8
3496568-rw-r -- 2 root 17 09-25 14:48 w
3496568-rw-r -- 2 root 17 09-25 14:48 w_hardlink
[Root @ www.bkjia.com water] # cat w_hardlink
My name is www.bkjia.com
Restrictions:
1) because an inode number is shared, both files must be on the same file system.
2) The hard link to the directory cannot be created.
3 soft links
Definition:
The soft link reads the content of the object to which it is directed.
Features:
1) the soft link is a separate file different from the original file (the inode number is different)
2) the soft link file type flag is l
3) the permissions of the original file will control the access to the file, which is irrelevant to the permissions of soft links.
4) the soft link size is always equal to the total number of characters in the path name of the original file.
Syntax:
Ln-s soft link of the original file
Case:
[Root @ www.bkjia.com water] # touch w
[Root @ www.bkjia.com water] # cat> w
My name is www.bkjia.com
[Root @ www.bkjia.com water] # ln-s w w_softlink
[Root @ www.bkjia.com water] # ll-I
Total 4
3496568-rw-r -- 1 root 17 09-25 15:12 w
3496973 lrwxrwxrwx 1 root 1 09-25 w_softlink-> w
[Root @ www.bkjia.com water] # cat w_softlink
My name is www.bkjia.com
Finally, we will summarize the soft links and hard links in one sentence:
Soft link: it is equivalent to a shortcut. You can create soft links for all files.
Hard link: It is equivalent to an alias for the file. The linked file and the source file are actually the same file. Cannot be used as a directory.
Recommended reading:
Linux hard links and soft links
Create a soft link command in Linux-ln-s