Hard Link vs. Symbolic Link

Like kids in elementary playgrounds, Linux has a way to create nicknames for a file. These nicknames are called links and there are not one, but two different type of links: the Hard Link and the Symbolic Link.

First what is an inode?

An inode is basically a map to all the file’s data content, permissions, and etc. There is an associated number or address to the inode for a given file.

What is a Hard Link?

A hard link is a file that points to the inode of a pre-existing file. So to be clear, more than one file can point to the same inode address.

To create a hard link:

creating a hard link

To create a hard link, the ln command is used. The first field is the pre-existing file you want a link to be created for and the second field is the nickname you want the new file to have. By default, the ln command creates a hard link.

This will create a new file, nickName, with its data content linked with originalFile. While it sounds like I am making a copy of originalFile, this is not what is happening. I have created a file, nickName, that is linked to the inode of originalFile.

created a new file named nickName

This means that if I change any content of originalFile, the contents of nickName will also change.

changing content

Now, if you decide to remove originalFile from your directory, nickName will still exists, including the most updated content of originalFile. Thus, you really have not deleted the file since the data that the inode has mapped still exists.

What is a Symbolic Link?

A Symbolic Link is special file that points to another pre-existing file.

To create a symbolic link:

creating a symbolic link

Creating a symbolic link is similar to creating a hard link but the ‘-s’ flag has been placed to let the ln command know you are creating a symbolic link. This command will create a file, symbolicLink, that points to the path originalFile. Unlike the hard link, the symbolicLink does not contain any data, information, or content of the originalFile. It merely leads you to your originalFile.

Because a symbolic link does not copy and data or information from the originalFile, this allows the user to also create symbolic links to directories also, unlike hard links.

Now, if you decide to delete your originalFile or move it to another directory, your symbolic link will cease to function since when you created your symbolic link it remembers the location of your file.

removing originalFile, also makes your symbolic link cease to function