Home >Operation and Maintenance >Linux Operation and Maintenance >What is the difference between symbolic links and hard links in linux
Difference: 1. File renaming or file movement will not change the link direction when using a hard link. File renaming or file moving when using a symbolic link will break the link; 2. Hard links can only link files, symbols Links can link files and folders; 3. Symbolic links can be created across different file systems, but hard links cannot be created across different file systems.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
There are two different types of links in Linux, soft links and hard links. Modify one of them. The hard link points to the node (inode ), the soft link points to the path
Soft link file is also called a symbolic link. This file contains the path name of another file, similar to the shortcut under win
File renaming or file moving
File renaming and file moving are both for Linux systems Change of absolute file path. For hard links, file renaming or file movement will not change the link direction, while for soft links, file renaming or file movement will break the link. At this time, when the file content is modified through the soft link, a new one will be created. The new inode is associated with the original file name and file data block.
File deletion
The rm command or the unlink of nodejs actually reduces the number of inode links by 1. For the previous hard link, delete test_hard.txt so that the link number of inode1 becomes 1. When the link number becomes 0, the system will release the inode, and new files created later can use the inode number of the inode. . At this time, there is no inode pointing to the file data block, so the file cannot be found. But in fact, the file data is still stored in the hard disk, so you can often see some tools on the Internet to help recover accidentally deleted files. The number of soft link inode links is 1. If the soft link is deleted, the system will release the inode.
Linking files and folders
Soft links can link files and folders, but hard links can only link files.
Create links in different file systems
Soft links can be created across different file systems, but hard links cannot, because hard links share an inode and different files Systems have different inode tables.
Hard link
File backup: In order to prevent important files from being accidentally deleted, file backup is a A good idea, but copying files will consume disk space. Hard links can realize file backup without taking up disk space.
File sharing: When multiple people jointly maintain the same file, they can create a hard link in a private directory through a hard link. Everyone's modifications can be synchronized to the source file, but it also prevents one person from accidentally The problem is that if you delete it, you will lose the file.
File classification: Different file resources need to be classified. For example, if a movie is classified as foreign and suspense, then we can create hard links in the foreign folder and the suspense folder respectively, so that Avoid wasting disk space by duplicating movies. Some people may say, isn’t it also possible to use soft links? Yes, but not in a good way. Because once the source file is moved or renamed, the soft link becomes invalid.
Soft link
Shortcut: For files with deep paths, it is not convenient to find them. Use soft links to create shortcuts on the desktop to quickly open and edit files.
Flexible switching of program versions: For programs that have multiple versions on the machine at the same time, you can quickly switch program versions by changing the direction of the soft link. It is mentioned here that switching the python version can be done in this way.
Dynamic library version management: I don’t know much about it, you can read here for details.
Summary
The Linux system manages files through inodes, which store information such as the number of file bytes, file permissions, number of links, and data block locations.
The hard link shares the inode with the source file. Except for the different file name, everything else is the same as the source file. Hard links cannot be created to folders, and hard links cannot be created to files in different file systems.
Soft links are similar to windows shortcuts and have independent inodes. Soft links can be created to folders or files on different file systems.
The modified file contents of hard links and soft links will be synchronized to the source file, because essentially they are all data blocks pointing to the source file.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of What is the difference between symbolic links and hard links in linux. For more information, please follow other related articles on the PHP Chinese website!