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.

Sandclock 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, Sandclock 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

Introduction to Lucene

In this lesson, we will understand the workings behind one of the most powerful full-text search engine, Apache Lucene....
28/12/2020

What Is a Boot Loader?

Although boot loaders tend to be very small and relatively simple, they play a critical role in the boot process. Visit...
28/12/2020

How to Use Manjaro Hardware Detection Tool

Manjaro is based on the well-known Arch Linux. Among all the facilities, Manjaro Hardware Detection Tool is a unique one....
29/12/2020
Bài Viết

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

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021