Hard links and Symbolic links, What is that?

Carolina Hernandez Viveros
4 min readSep 16, 2020

To understand a little better about this topic, we must start with something important, the inodes.

An inode o index file is like a database of a file so it contains many information about the file how inode number (very important), file size, owner information, permissions, file type, among others.

SYMBOLIC LINKS

A symbolic link aka soft link it’s like shortcuts in windows, a pointer to the original file but with a different inode number and file permissions and these cannot be updated, and has only the path of the original file, not the contents. What does this mean? that at the time of deleting the original file the symbolic link would be useless for not having a reference point.

Description of the symbolic link

HARD LINKS

A hard link is a mirror copy of the original file, the same inode, size, contents of the original file, but a different name, so if you delete the original file the hard link will still have the data from the original file. The permissions are also the same and these will update if we change the permissions of the source file.

Description of the hard link

To understand them better, let’s do a practical example.

Creating Soft Link or Symbolic Link

In this example, we have created a file with a .txt extension which contains the phrase “Hello World”.

vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ echo "Hello World" > file1.txtvagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ cat file1.txt
Hello World

Now, to create the symbolic link we use the ln command, this command creates a link between files and we add the option -s — symbolic to create the symbolic link, in this way we create a link to file1.txt with the name softlink.txt, as we can see both files have the same data.

vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ ln -s file1.txt softlink.txtvagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ cat file1.txt
Hello World
vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ cat softlink.txt
Hello World

Now we are going to list these files showing the inode of each one, the option for the ls command is -i, but we also use the -l option to list in long format and see the detailed file data and -a to show hidden files, as we observe the inode of, the permissions and the size are different.

vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ ls -lia
total 12
268251 drwxrwxr-x 2 vagr vagr 4096 Sep 16 03:04 .
262157 drwxrwxr-x 5 vagr vagr 4096 Sep 16 02:56 ..
268919 -rw-rw-r-- 1 vagr vagr 12 Sep 16 03:03 file1.txt
268920 lrwxrwxrwx 1 vagr vagr 9 Sep 16 03:04 softlink.txt -> file1.txt

Finally, we are going to delete the original file, we verify the data of softlink.txt and we see that there is no such file.

vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ rm file1.txt
vagrant@vagrant-ubuntu-trusty-64:~/Holberton/test$ cat softlink.txt
cat: softlink.txt: No such file or directory

Creating Hard Links

For this example we also use a .txt file that contains the phrase “Hello World” and we create the hard link with the ln command (this time without any option) in this way we create a link to source.txt with the name hardlink.txt, as we can see both files have the same data.

Now we list the files in long format to see the inodes, and we see that the inode, the permissions, and the size are the same in both files, to finish we delete the original file, we verify the content of the hardlink.txt file, and when listing we see that it still has its inode, permissions, size, and data intact.

I hope that with this explanation you have a basic idea about the use of symbolic or soft links and the hard link and how to create them.

Thanks for reading to me !!!

--

--

Carolina Hernandez Viveros

Software development student at Holberton School, lover of science, technology, cooking, music, cats and nature. Also geek