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


A hard link and soft link being created with the target file of test.txt

What is a hard link?

A hard link is essentially another name for a file. This way we can have a bunch of different names that all reference the contents of a single file. When any of these names are acted upon, the original file they are linking to is acted upon as well.

How do you create a hard link?

A hard link is created using the following syntax:

$ln File Link

Substitute “File” with the name of the file that you’d like to link to, and substitute “Link” with the name of the hard link that you’d like to use and you got yourself a hard link.

Why use hard links?

Hard links can come in handy when you want to use a file, such as a script that automatically pushes your code to GitHub, in a directory that doesn’t contain that file. However, hard links do have their limitations. For one, they cannot be created for directories themselves. Additionally, hard links cannot reference files located on separate computers. This brings us to symbolic links.

What is a symbolic link?

A symbolic link, or a soft link as some call it, is a file that references or links to another file in our system. The important distinction between a hard and symbolic link is that a symbolic link does not contain the data of the target file, whereas a hard link does.

How do you create a symbolic link?

A symbolic link is created using the following syntax:

$ln -s SourceFile SymbolicLink

Again, replace “SourceFile” with the name of the file you want to link to and replace “SymbolicLink” with the name of the symbolic link you want to create. You may have noticed we are using the same “ln” command that we used for creating a hard link. This is our linking command, the option of “-s” is what tells Bash to make this a symbolic link and not a hard link. If you forget the “-s” then you will get a hard link.

Why use symbolic links?

Symbolic links can come in handy when you want to link to directories or computers connects by NFS. Again, symbolic links do not contain the actual data of the target file as a hard link would. This is why if a target file is deleted or moved, a symbolic link will no longer function properly whereas a hard link will.

A soft link attempting to be copied after the file it was linking to was removed. Notice how we get an error telling us SoftLink no longer exists.

A quick note on inodes

An inode is a data structure that describes a file or directory. It carries metadata such as the file’s owner ID, group ID, size, number of hard links, etc. Directories are really just files that contain a table which lists all files and their assigned inodes. A file’s inode can be found using the following command:

$ls -i

When you hard link to a file, you are assigning another name to a file’s inode. This new name is just as important to the kernel as the original file’s name because they both share the exact same inode. In contrast, when you make a symbolic link, it has a different inode altogether because symbolic links are dependent on the file’s name not the inode.

A listing of a file test.txt and a hard and soft link to test.txt. Notice how the hard link and test.txt share the same inode, while the soft link does not.
Like what you read? Give Wescott Sharples a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.