How to Use Linux ln Command

29/12/2020
Chưa phân loại
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.

  • Hard links can’t be created for directories. It can only be created for files.
  • Hard link of a file residing in a filesystem can’t be created in a different filesystem.
  • Hard link residing in a partition can’t be created in a different partition or storage devices.

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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Configure Git Server with HTTP on CentOS 8

In this article, I am going to show you how to configure Git Smart HTTP server on CentOS 8 for hosting your Git repositories...
29/12/2020

Install Linux Mint Cinnamon

Within the maze of Linux distributions, Linux Mint is one of the top ones. Personally, I love Linux Mint. Linux Mint is...
29/12/2020

[Checkmk 1.6] Hướng dẫn cài đặt

Checkmk là một giải pháp giám sát mã nguồn mở có khả năng giám sát, cảnh báo và hiển thị các...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024