I’m here and also there

Roberto Ribeiro
IT student life
Published in
4 min readSep 19, 2020

A little bit about links in Linux environment

Hard and symbolic link example

Introduction

Today we will try to explain the difference about hard links and symbolic links in the Linux environment. If you come from Windows we will explain how they differ from shortcuts.

A bit of theory

To begin with, we must know that in Linux each file and folder of the operating system is assigned an integer called an inode.

An inode is a record on the disk. Each inode is unique for each of the files and each of the folders. The number of inodes in your user equals the number of files and folders that are stored in the account. The construction of this concept in the early days of computing was aimed at recovering damaged file systems.

The information that each one stores is as follows:

  • The permissions of the file or folder.
  • The owner of the file and folder.
  • The position/location of the file or folder within our hard drive.
  • The date of creation of the file or directory.
  • Among other things

However, they do not save the name and content of the files. This is where the concepts of hard links and symbolic links come in.

What is a hard link?

A hard link is a file that points to the same content stored on disk as the original file.

In other words, the original files and the hard links will use the same inode and both point to the same content stored on the hard disk. It is a way to identify a content stored in the hard disk with a different name than the original file.

Their characteristics are

  • The same file can have different names
  • They are the same size
  • They have the same inode number
  • Any changes made to the original file or the hard link affect both equally.
  • In the case of deleting the original file we can still access the content through its hard link. The content will not be deleted until both the original file and all hard links have been completely removed.
  • Hard links cannot be created from folders
  • Hard links take up less space on the hard disk than symbolic links.
  • Access to content via a hard link is faster than with symbolic links
  • Can only be used on the partition on which we have created them.
  • If we change the location of the original file the hard link is not broken
  • And finally, the permissions, the owner and the group of the hard link will be the same as the original file

I will explain how they are created

For this case I created a file called “archive” (I was not very creative)

To find out the inode number of this file we use the “ls” command with the “-li” parameter.

The first number that appears refers to the inode number of this file, in this case is “12720874” which is in red

To create a hard link we use the “ln” command and its syntax is “ln original new_link_file”.

In the example we can see how I created a “file” hard-link with the name “hd_file”.

If we make again “ls -li file” we can see that it tells us that this file has more than one link. In the example they are the circles that are in blue.

What is a symbolic or soft link?

Symbolic links are similar to shortcuts in Windows and are the links that all users are used to using.

Their characteristic is:

  • They point to the original file
  • Have different number of inodes
  • If the original file is deleted, the file is completely deleted and the link is broken.
  • If we delete the link, the original file still exists.
  • You can create folder links
  • Can be used on any location, partition and file system on our hard drive.

This is how symbolic links are created

To create symbolic links, use the “ln” command. Its syntax is: “ln pathfile pathnewfile”.

In the example, create a file called “file” (I know it’s not very original, I recognize it again) and use the command “ls -li” to see the files that are referred to it.

Using the command that creates the symbolic link “ln -s /home/roberto/file /home/roberto/Escritorio/filesl” created a new file called “filesl” on my desktop.

Finally I use “ls -li” again where we can see that the symbolic links are not counted, as we can see in the blue circles of the example.

Summary

In this way it is explained that are the symbolic links and hard links, their use and how we create them.

I look forward to your comments.

--

--