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

Hardlinks denoted with blue arrows. Symbolic links denoted in red arrows.

In order to know about the differences between a hard link and a symbolic link you first have to understand how the Linux operating system keeps track of all the files and directories. Within the Linux filesystem a datastructure called the inode stores all the information about a file except its name and its actual data. Every file and directory has an entry within the inode denoted by an inode number.

Corresponding inode number (boxed in red) of each file

Now that is cleared up, it will be much easier to understand what a hard link and a soft link is.

A hard link is a link that references the same inode entry as the original file. When the original file was created, a hard link was formed. Every inode entry keeps track of the number of hard links referencing said entry, and should the count reach 0 (resulting from the deletion of the original file along with all the hard links) the inode entry is deleted.

A symbolic link is a link that references the original file. Unlike a hard link, you are allowed to have symbolic links that reference a directory. The deletion of all symbolic links that point to a single inode entry will not result in the deletion of the inode entry. However the deletion of the file which the symbolic links references, will render the link useless.

To create these links we use the ln command.

ln source link-name

The resulting command will lead to the creation of a hard link. source is the name of the file and link-name is the name of the hard link you wish to give it.

ln -s source link-name

With the option -s, ln will instead create a symbolic link.

To delete the links we use the command

rm link-name
Like what you read? Give Teng Alex Yu a round of applause.

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