Linux 101 : Symbolic links and Hard links



Linux offers us a way to create "pointers" to files. These "pointers" usually have a different name than the file they are pointing to.

There are two types of "pointers" or links.

A symbolic link,  is a new file that points to the original file. The two files are similar in terms of their content.
A file and its symbolic link can live on two different partitions or two different storage devices.

Below is an example of how to create a file - with the "touch" command - and its symbolic link "symbolic_link_to_file":


We can display our files and their inodes - an inode is a structure that stores information about files, each file has one inode -:


The "symbolic_link_to_file" file  "->" points to the original file named "file".

We could see that the symbolic link has a different inode number "543" than the original file "542", because they are two different files with different permissions as we can see in the "ls" command output.

If we change the permissions "rw-r--r--" of the original file, they are not reflected in its symbolic link.

Remark:

The symbolic link file has the character "l" in the above output, which stands for "link".

If we remove the original file "file", we can't access it through its symbolic link anymore. 

The hard link on the other hand is just a "pointer file" that points to the original file. They are the same "unit".

Below is an example that shows us the process of creating a hard link to the file "file":


We display our files and their inodes as follows:


Both files, the original one and its hard link have the same inode number "542" and the same permissions "rw-r--r--", which means that they are the same "thing".
If we change the content of a file, it is also reflected in its hard link.

A hard link is tied to the inode of the original file. 
A hard link and the file it points to are on the same partition.

Remark:

If we remove the original file "file", we could still have access to its content through its hard link

Comments

Leave as a comment:

Archive