Soledad Frechou
5 min readSep 21, 2020

--

What is the difference between a hard link and a symbolic link?

In every file system it is common to work with — and on — more than one file at a time. Copying and pasting, duplicating and deleting files are all actions carried out constantly when updating and creating new work.

In Linux, which is a file system based entirely in directories and files, copying and pasting files can be easily done to duplicate a file. The outcome will be two sets of the same file, independent one from the other. If one is deleted or edited, those actions will not affect the copy.

In some cases, it will be not enough or useful to copy and paste files, but instead it will be necessary to create a soft or hard link to said files.

But- what is a soft link and a hard link and why should they be used?

Before getting into the actual definitions and comparisons, there is some other relevant information that should be defined first. To comprehend the use of space disk and how files are actually stored and referenced by the operating system, we must first go into Inodes.

Inodes

Inodes are basically identification cards for files. These contain all the information about a file, such as size, permissions, date of creation, owner and more. The way an operating system knows where a file is located and what that file is, is not by the file name but by the file’s Inode number. To store this information, the operating system creates a set of tables called Inode Tables in which Inode Numbers are stored. It is through these tables and numbers that when as a user you reference a file by name, the system will look through the Inode Tables, find the Inode Number related to that file name, and will only then identify the file you need and interpret its contents.

Every time a new file is created it is assigned an Inode Number and a name, so that both the operating system and the user can identify said file.

To output the Inode Number for your directory’s files, you can prompt ls — — inode. The result will be a list of the files contained in that directory with the corresponding Inode Number for each file.

Soft Links

A soft link acts as a link that points to the original file. It works like a shortcut that leads to the file and is not an actual file itself. A soft link has a different Inode number that the original file, and they are often used as faster access to files located in another file system. This could be done because the path to that file is a complicated one for example, or there are disk space issues in one file system so as to bring the original file to it.

They act as windows to the file in order to access the content, but don’t actually contain it. It is a path to the original file. This means that if the original file is deleted, then the soft link becomes useless because it has nothing to link to. When content is accessed through a soft link and edited, it is edited on the original file and the soft link is just the access to it.

Hard links

On the other hand, a hard link is a mirror image of the original file in a separate file but shares the same Inode number. This means that if content is edited in one of the files, it will mirror the changes on the other file because they are essentially the same file, with two instances of the same file in the file system.

Nevertheless, a hard link does not occupy double the disk space like copying or duplicating a file does. Also, hard linking differs from copying a file in that content is synced through the link. In a copied file, the content is independent between files, even though it starts as the same, it can be changed and remain different one from the other.

Unlike the soft links, the hard link or the original file can be deleted in a hard link relation between files, without affecting the other file. If the original file is deleted, the content can still be accessed through the hard link.

Another difference with soft links is that hard links cannot cross the file system boundaries and will only work within the same file system. Also, hard links cannot link directories whereas soft links can.

Visual representation of soft links and hard links with the Inodes

Creating and deleting soft and hard links

Soft links and hard links are created through a command prompt.

ln filename link-name will create a hard link of that file, while ln -s filename link-name will create a soft link for the file.

Soft links and hard links can be removed using the unlink command or simply removing them as you would any other file with the rm command.

Proof of hard links and soft links through inodes:

To display the inodes for the files, you can use de -i argument of the ls command.

As stated before, soft llinks will have different Inodes than the original files while hard links will share the same Inode with the original files.

The ls -il command will also show that besides having different Inodes, soft links also have an “l” before the permissions, and these are different from the original file too, while in hard links they remain the same.

Conclusion

Soft links and hard links are useful relations between files that will make it easier for the users to access files and run programs in the file system. It is important to know the key differences between one and the other in order to use them properly and understand the potential they have for disk space and file manipulation.

--

--