understanding file linking: hard link vs symbolic (or soft) link in linux

In Linux and Unix operating systems, file linking is a process by which a single file is referred to by multiple names from various locations. You can essentially think of them as shortcuts or pointers, something that you can create to avoid maintaining multiple copies while still accessing the same file content from various folders by different names.

It is also possible to create links in Windows NTFS file system, but we will only deal with Linux and Unix operating system in this post.

Two types of links

There are two different types of links that you can create: Hard Links and Soft Links. Soft Links are also known as symlinks or symbolic links. Symbolic links are usually more widely used than the hard links.

In order to understand the difference between the two types of links, you need to have a little understanding of how files are stored in the file system. The files have mainly three different parts: the user data (or actual file content), the file name and the metadata. Linking is the process by which a different file names are mapped to the same data or content, and the associated metadata.

Just for clarity and as a disclaimer, the inode table that holds the metadata also holds the location of user data but for this explanation, treating them as three different entities (instead of two) makes it a little bit easier.

There are two different ways you can go about creating the link, which corresponds to the two different link types.

What is soft link?

One way to create this kind of a mapping is to treat it like a shortcut. This method creates a special file which does not have any user content but has the information (ie. path and name) about the file name it links to, which in turn knows about the content. This type of linking is known as symbolic linking or symlinks. In this case, the link has an inode for itself but links to the original file name object to access the user content.

In the case of a symbolic link, either deleting or moving the original file will break the link in the sense that it cannot access the content. But the link object itself will exist as a (broken) file as it has its own inode entry.

The existence of a symbolic link has no relevance on the deletion of the user content and inodes/metadata.

Pros

  • You can create symbolic links to almost all file system objects, including files, directories, devices etc. You can even create one to an imaginary file or one that does not exist (yet!)
  • Links can be created across file systems and even across disks or mounts.
  • Easier to find what the file actually links to or what and where the original file was. The link can be seen with the ls command.
  • Ability to have separate or different metadata such as file permissions for the symbolic link than the original file.

Cons

  • Deletion or moving the files causes the symlinks to break.
  • Some softwares do not work with symlinks and requires an actual/physical file. This could also be for security reasons in some cases.
  • Permissioning is a little more complex, as both the link and file permissions are required to perform CRUD operations.

What is a hard link?

Another way to do the mapping or linking is the create a brand new file name object, but associate it with the existing content and metadata by providing it with the same inode number as the original file name. This method is referred to as creating a hard link to the file. In this method, the hard link itself does not "know" about any other or the original file name and behaves as if none exist. It is the file system that is responsible for managing the file names, file counts and other related information.

Pros

  • Deleting the original file does not cause the link to break or the content to disappear.
  • No separate inode is required as it is shared between the link and the file. This is much more efficient than maintaining a different inode for the link object.

Cons

  • Cannot link across file systems. This is because each file system has its own inode table, so sharing inodes using numbers will not work as these ids might not be the same across inode tables.
  • Cannot link directories easily…again as the inode table entries are to be shared.
  • It is much harder to keep track of the hard links unless you want to search using the inode numbers.

Both types of links has its own advantages and disadvantages depending on the scenario. The requirement will often dictate as to which one you should use. I prefer to use the symbolic links most of the time as it is easier to keep track and maintain in the long run. Unless a hard link is absolutely necessary and called for stick with the symbolic links.

Creating a symbolic link

Both the hard and symbolic links are created using the ln command. By default the ln command creates a hard link. In order to create a symlink or the symbolic link you have to specify the –symbolic (-s) command line option.

bash$ ln -sfn <file path> <symlink path>

Example:

bash$ ln -sfn /opt/firefox-nightly/firefox/firefox /opt/bin/myfirefox

The above command creates a symbolic link called myfirefox in /opt/bin folder/ which points to the firefox script in the installation folder /opt/firefox-nightly/firefox.

Creating a hard link

bash$ ln -n ~/temp/fileone /opt/bin/hardlink

This creates a hard link called hardlink to the file fileone.

Linking, especially symbolic linking when used effectively can be a good substitute to copying and maintaining multiple copies of the files in different folders.