Hard links and Symbolic links on Linux

Tram Ho

Introduce

  • In the Linux file system, a link is a connection between the file name and the actual data on the disk.
  • There are two main types of links that can be created: “hard” links, and “soft” or symbolic links. Before learning about hard links and symbolic links, there is another concept that needs to be understood as “inode” – a basic concept in the Linux filesystem. Each filesystem object is represented by an inode.

1. Inode

  • In Linux, the data of files is divided into blocks. There are many ways to organize blocks of data in a file, one of which is using indexed allocation.
  • In an inode, there are the following metadata:
    • File size in bytes.
    • Device ID: the id of the device to save the file.
    • User ID: The owner id of the file.
    • Group ID: The group id of the file owner.
    • File mode: including file types and file access methods.
    • Timestamps: timelines when: the inode itself is changed (ctime, inode change time), the content of the file changes (mtime, modification time) and the latest access (atime, access time).
    • Link count: the number of hard links pointing to the inode. Pointers point to blocks on the hard drive used to save the file contents. The cursor indicates where the file is to read the content.
  • Inode identifies the file and its attributes (attributes listed above). Each Inode is identified by a unique number in the file system.
  • INODE is:

Inode is a data structure in traditional file systems of Unix families such as UFS or EXT3. Inode stores information about a common file, directory, or other file system object.

  • There are two notes in the content of inode:
    • Inode does not contain filenames and directories.
    • The cursor is the most important component: it indicates the address of the block where the file content is stored and which blocks the content of the file can be accessed.

2. Hard links

  • Hard links are low-level links that the system uses to create components of the file system itself, such as files and directories. Hard links will create a link in the same file system with two corresponding inode entries pointing to the same physical content (the same number of inodes because they point to the same data).
  • All file-based file systems must have at least one hard link (link counts of 1 or more) providing the original name for each file.
  • The command to create a hard link is as follows: ln [file nguồn] [file đích]

  • 2 files viblo.txt and hardlink.txt have the same inode number 1326632. Delete viblo.txt file, the content of hardlink.txt file will remain.

  • The content in hardlink.txt still remains because when deleting the viblo.txt file, the system only removes the number of link count in the file’s inode 1.
  • When using the rm command to delete a file, it reduces a hard link. When the number of hard links decreases to 0, it is no longer accessible to the contents of the file

3. Symbolic links

  • Most users don’t want to create or modify hard links themselves, but symbolic links are a useful tool for any Linux user.
  • Symbolic links are special files that point to another file or directory – called the target . Once created, a symbolic links can be used instead of the target file. It can have a unique name, and be placed in any directory. Many symbolic links can even be created for the same target file, allowing access to the target by different names.
  • The symbolic link does not contain a copy of the target file’s data. It is similar to a shortcut in Microsoft Windows: if you delete a symbolic link, the target will not be affected. Because it is merely a shortcut, the symbolic link does not use the inode entry. It will create a new inode and the content of this inode points to the original filename.
  • Additionally, if the target of a symbolic link is deleted, moved, or renamed, the symbolic link is not updated. When this happens, the symbolic link is called “broken” or “orphaned” and will no longer act as a link.
  • The command to create a symbolic link is as follows: ln -s [file nguồn] [file đích]

  • 2 files viblo2.txt and softlink.txt have different inode numbers of 1326634 and 1326630 respectively. Delete vibo2.txt file, the content of softlink.txt file will be no longer available.

  • The content of softlink.txt cannot be displayed because softlink.txt points to another file, which does not exist.

4. Compare Hard links and Symbolic links

Hard linksSymbolic links
Only links to the file, not the directoryCan link to directory
Cannot refer to file on another driveCan refer to other file / directory drive
The link to a file remains even after the file has been movedLinks are no longer referenced if the file is moved
Linked to the physical reference inode on the hard drive where the file is storedLinks refer to abstract file / directory names that are not physical addresses. They are provided with their own inode
Can work with any applicationSome applications do not allow symbolic links

References

https://en.wikipedia.org/wiki/Inode

https://www.computerhope.com/issues/ch001638.htm

https://www.nixtutor.com/freebsd/understanding-symbolic-links/

Share the news now

Source : Viblo