File Management

How to Create Hard Link and Soft Link in Linux?

How to Create Hard Link and Soft Link in Linux?
In the Linux operating system, all the information about a file is stored in its respective inode. These inodes allow you to know all the metadata of a file. There is a concept of creating links to a file in Linux, just like we create pointers to the files in most of the popular programming languages. These links are basically of two types: the hard and the soft links. A hard link to a file is essentially an exact copy of the file, which means that a hard link to a file and the actual file will share the same inode. The biggest advantage of creating a hard link is that even if you accidentally delete the actual file, you will still be able to access its contents via its hard link.

On the other hand, a soft link or a symbolic link works exactly like a pointer or a shortcut to a file. It is not an accurate copy of the file but only points to the original file. A soft link to a file and the actual file will have different inode values. Moreover, if you delete the actual file at any time, you will not be able to access its contents via its soft link. Today, we will share with you the methods of creating a hard link and soft link to a file in Linux.

Note: We have used Linux Mint 20 for walking you through the methods shown below.

Method of Creating a Hard Link in Linux Mint 20:

For creating a hard link to a file in Linux Mint 20, we will perform the steps mentioned below:

First, we need to launch the terminal so that we can pass commands via the terminal in Linux Mint 20 for creating a hard link. We have also attached the image of the Linux Mint 20 terminal below:

Now you need to list down all the files present in your current working directory. You can also do this for any other directory of your choice by explicitly specifying its path. However, we preferred working with the Home directory so that we do not have to mention its path in our command below:

$ ls -l

This command will list all the contents of the working directory you are currently working on.

The contents of our Home directory are also shown in the following image:

Now select any file of your choice whose hard link you want to create. We have highlighted our selected file in the image shown above. Now we will attempt to create a hard link for the file named “awk.txt” by running the command shown below:

$ ln awk.txt abc.txt

Here, the first file will be the one whose hard link you want to create, whereas the second file specifies the name of the hard link to be created. You can use any name other than “abc.txt”.

After running this command, you need to list down the contents of your current working directory once again with the “ls” command to verify if a hard link to the file “awk.txt” has been created or not. Once you run this command, you will be able to see the exact clone of “awk.txt” with the name “abc.txt” in your current working directory as highlighted in the following image:

Another way to verify it is to visit your Home directory by clicking on your File Manager icon. There, you will be able to see “awk.txt” along with “abc.txt” which is, in fact, the exact copy of the same text file. You can also open both of these files to verify if their contents are the same or not.

Method of Creating a Soft Link in Linux Mint 20:

For creating a soft link to a file in Linux Mint 20, we will perform the steps mentioned below:

We need to run the “ls” command once again to check the contents of the current working directory, as we did in the method shown above. This time, we have selected another file named “Bash.sh” for creating its soft link as highlighted in the following image:

After selecting a file for creating its soft link, we need to execute the below-mentioned command in our Linux Mint 20 terminal:

$ ln -s Bash.sh NewBash.sh

Here, the “-s” flag indicates that we are going to create a soft link to a file; the first file refers to the file whose soft link is to be created, whereas the second file refers to the name of your soft link or the pointer to your first file. You can have any name of your choice for the soft link of this file.

After running this command, you need to list down the contents of your current working directory once again with the “ls” command to verify if a soft link to the file “Bash.sh” has been created or not. Once you run this command, you will be able to see the soft link named “NewBash.sh” which will be pointing to the file named “Bash.sh” in your current working directory, as highlighted in the following image. Moreover, you will also be able to see the “l” flag in the highlighted entry, which further indicates that the link you have just created is merely a pointer to the file and not the exact copy of the file.

Another way to verify it is to visit your Home directory by clicking on your File Manager icon. There, you will be able to see “Bash.sh” and “NewBash.sh” which is, in fact, the soft link to the former file. You can also see an arrow located on the “NewBash.sh” file, which shows that it is kind of a shortcut or a link to the file “Bash.sh” and not its exact copy as highlighted in the image shown below:

Conclusion:

By explaining to you the methods of creating the hard links and soft links to a file in this article, we have enabled you to create either of these links to any file of your choice according to your requirements. We have tried to demonstrate these methods in the easiest way possible.

Gry Najlepsze dystrybucje Linuksa do gier w 2021 r
Najlepsze dystrybucje Linuksa do gier w 2021 r
System operacyjny Linux przeszedł długą drogę od pierwotnego, prostego, serwerowego wyglądu. Ten system operacyjny znacznie się poprawił w ostatnich l...
Gry Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
W przeszłości granie w gry było uważane tylko za hobby, ale z czasem branża gier odnotowała ogromny wzrost pod względem technologii i liczby graczy. P...
Gry Najlepsze gry do grania ze śledzeniem rąk
Najlepsze gry do grania ze śledzeniem rąk
Oculus Quest niedawno wprowadził świetny pomysł śledzenia rąk bez kontrolerów. Przy stale rosnącej liczbie gier i działań, które wspierają zarówno for...