Comenzi Linux

How to Use Linux ln Command

How to Use Linux ln Command
In Linux, usually the ln command is used to create symbolic links of a file or directory. If you need to have the same file or directory in multiple file paths, then symbolic link is the easiest solution available. Symbolic links requires only a few kilobytes of disk space.

There are two types of symbolic links, hard links and soft links. The ln command can be used to create both hard and soft links.

Hard Links:

These are just another name for the same file. You can also call it an alias. Hard links have the same inode number as the original file. So, they are not really any different from the original file other name having a different name.

Hard links has some limitations.

Soft Links:

Soft links basically overcomes the limitations of hard links. You can create soft links of files and directories. Soft links can point to files and directories residing in different filesystems. Soft links can also point to files and directories residing in different partitions and different storage devices, even network mounts.

Unlike hard links, soft links has their own inode numbers. So, they are different files or directories than the original files or directories.

The disadvantage of soft links is, if the original file or directory is renamed, moved or removed the link is broken. In this case, you will have to redo the link manually.

The ln command is available in every Linux distribution by default. So, you don't have to install it separately.

In this article, I am going to show you how to use the Linux ln command to make soft and hard symbolic links. I will also show you how to manage soft and hard symbolic links created with the ln command. So, let's get started.

Creating and Removing a Hard Symbolic Link:

To make a hard link of a file (let's say test.txt), run the following command:

$ ln test.txt test_hl.txt

NOTE: Here, test_hl.txt is a hard link to the file test.txt.

Now, if you check the inode number (33577665 in my case) of the files test.txt and test_hl.txt, you will find that both of the files have the same inode numbers. This is expected as we've created hard link of the file test.txt. So, the link file test_hl.txt also has the same inode number as the test.txt file.

$ ls -li

The number 2 here resembles the total number of files with the same inode number. In our case, we have 2 files with the same inode number.

If you wanted, you could put the hard link to a different directory as well as follows:

$ ln test.txt /dir1/dir2/test_hl.txt

To remove the hard link test_hl.txt, run the following command:

$ unlink test_hl.txt

As you can see, the test_hl.txt hard link is no longer listed.

Creating and Removing Soft Links:

To create a soft link of the file test.txt, run the following command:

$ ln -s test.txt test_sl.txt

As you can see, the soft link test_sl.txt of the file test.txt is created. In ls listing, the soft link uses -> sign to show the link to the original file. Also, note that the inode numbers of the original file test.txt (33577665) and the soft link test_sl.txt (33577676) is different.

You can make soft links of directories as well.

For example, to make a soft link of the directory /etc, run the ln command as follows:

$ ln -s /etc config

As you can see, the soft link config of the directory /etc is created.

You can also put the soft link of a file or directory in another directory as well. To do that, run ln command as follows:

$ ln -s test.txt /dir1/dir2/test_sl.txt

Or

$ ln -s test_dir /dir1/dir2/test_dir_sl

You can remove the soft link test_sl.txt as follows:

$ unlink test_sl.txt

Updating Soft Links:

At times, you may end up deleting the original file or directory without removing the soft link. This will leave the soft link broken. Or you may have made a soft link to a wrong file or directory by mistake.

In both cases, you can update the soft link very easily with the -f option of the ln command.

Let's say, you want to update the soft link test_sl.txt and link it to a new file hello.txt.

To do that, run the following command:

$ ln -sf hello.txt test_sl.txt

As you can see, the soft link is updated.

The same way, you can update soft links to a directory or files in another directory.

So, that's how you use the Linux ln command to make soft and hard symbolic links. Thanks for reading this article.

Battle for Wesnoth Tutorial
The Battle for Wesnoth is one of the most popular open source strategy games that you can play at this time. Not only has this game been in developmen...
0 A.D. Tutorial
Out of the many strategy games out there, 0 A.D. manages to stand out as a comprehensive title and a very deep, tactical game despite being open sourc...
Unity3D Tutorial
Introduction to Unity 3D Unity 3D is a powerful game development engine. It is cross platform that is it allows you to create games for mobile, web, d...