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

Vim Install Plugins

Ever heard the name of Vim? I bet you did! It’s one hell of a text editor. In fact, Vi/Vim is one of the longest-living...
29/12/2020

Mua proxy xoay US v4 v6 Giá Rẻ, Không giới Hạn Băng Thông

Mua proxy xoay US v4 v6 Giá Rẻ, Không giới Hạn Băng Thông Giới thiệu Proxy xoay US v4 v6 là một loại proxy...
28/12/2023

Lý do tại sao bạn nên sử dụng nhiều proxy đối với nhu cầu của mình.

Khi sử dụng proxy để lướt web, bạn có thể trải nghiệm nhiều lợi ích hữu ích như bảo vệ thông...
27/02/2023
Bài Viết

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

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

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025