Soft (Symbolic) Link vs Hard Link

29/12/2020
Symbolic links (also called soft links) and Hard Links are a resource to access files or directories from any location. This article will describe the differences between symbolic links and hard links.

Hard Links

Hard links are not a file containing the path to the original file but mirror copies of the original file they point to. A file and it’s hard links aren’t associated by the name or path but by the inode which stores information on the file, like it’s location, creation date, permissions and other attributes. Each inode number is unique within a filesystem preventing hard links from working between different partitions or systems. Hard links can’t be used to link directories.

In contrast to soft links, hard links contain the information they link to so if the original file is removed you can still access it’s data.

In all Linux systems both hard and symbolic links are created with the command “ln”. To get started first create a file called LinuxHintFile and a directory called LinuxHintDirectory as shown in the image below:

touch LinuxHintFile
rmkdir LinuxHintDirectory
ls
root@LinuxHint:/home/linuxhint#

To create a hard link called Link2LHFile  to the file LinuxHintFile run in the terminal:

ln LinuxHintFile Link2LHFile

After checking with ls we see the hard link we just created, if you type ls -i you’ll see the files’ inodes, you can confirm the link we just created has the same inode number than the original file, type:

ls -i

As we said above, we can’t use hard links for directories nor links to other volumes or filesystems, we’ll link LinuxHintDirectory with a soft link.

You can find hard links within the working directory by running:

find -type f -links +1

You can also find files by inodes, for this run:

find . -inum <InodeNumber>

As you see it found both the original file and the hard link sharing the same inode number.

 

Soft Links

In contrast to hard links, soft links are not copies of the original file, they contain the path to the original file, because of this if the original file is removed the soft link or symbolic link will point to no file becoming a broken link, or an orphaned link, which means if you loss the source file, if you delete or move it the symbolic link will loss access to the information, while with the hard link the information remains despite the source file removal because it is a full and exact copy of that file.

Also in contrast to hard links symbolic links don’t share the same inode with the original file, that’s why symbolic link can cross volumes and filesystems while hard links can’t. Symbolic links can be used to link directories while with hard links that’s not possible.

Symbolic links are also created with the program ln, but for the link to be symbolic/soft we need to specify the parameter -s, to create a symbolic link to the directory LinuxHintDirectory called LHDLink run:

ln -s LinuxHintDirectory LHDLink

And then run ‘ls -i’ to check it was properly created:

As we see in this case the symbolic link was created, but it has a different inode number than the original file.

We can list symbolic links within a directory with the command “ls -l

In the image above we can see in the permissions of the first file the prefix “l” for symbolic links, the end of the line also shows to what file the symbolic link points.

I hope you found this article useful to understand differences between hard links and symbolic links and how to create them. Keep following LinuxHint for more tips and updates on Linux.

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

Vimrc Tutorial

In this article, we’re going to dive deep into the vimrc file of Vim. Once you’re inside the vimscript, it’s easy...
29/12/2020

Hướng dẫn thêm tính năng chủ trì (host) trên Jitsi

Sau khi cài đặt Jitsi, mọi người đều có thể vào trang web của chúng ta để tạo phòng họp. Để...
30/12/2020

How to Zip a Folder in Linux

In this article, I will show you how to Zip and Unzip a Folder or Directory in Linux.  This might seem trivial, but sometimes...
28/12/2020
Bài Viết

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

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024