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

Python tempfile module

Often in our programs, we need to store some temporary information about the state of the program and objects which might...
28/12/2020

How to Disable IPV6 on CentOS7

How to Disable IPv6 on CentOS 7.5 IPv6 or Internet Protocol version 6 is the latest version of Internet Protocol (IP)....
28/12/2020

How to Install Sopcast Player 0.8.5 P2P Internet TV on Ubuntu 16.04

Sopcast Player is a simple, free way to broadcast video and audio or watch the video and listen to radio on the Internet....
28/12/2020
Bài Viết

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

Hướng dẫn fake ip bằng phần mềm SStap
10/06/2025

VPS treo game là gì? Thuê VPS treo game giá rẻ, không lo giật lag
02/06/2025

 BitBrowser – Best Anti-Detect Browser!
26/05/2025

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025