Understanding Symbolic Links and Hard Links in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two can help you effectively manage your filesystem.
Before starting to work with hard and soft links, let's first understand what a link is in Linux.
Every storage device contains files and directories in a collection of blocks.

Information about a file or directory is stored in a data structure called inode. Inodes contain metadata such as who is the owner of the file, when it was created and when it was last accessed, its size, permissions, ownership, and pointers to the data blocks where the file's contents are actually stored etc.
Every storage medium, such as hard disks, flash drives, etc., has its own inodes. These inodes are specific to the filesystem, storage device, and partition. So if we move one text file from the local storage device to the flash drive, it will get a different inode.
Every inode has an inode number that is unique and used by the filesystem to locate the inode.
A directory entry is essentially a table maintained by the filesystem that contains a name for a file or directory and a pointer (reference, often an address) to the inode number.
| Filename | Inode number |
| report.txt | 43132770 |
| image.png | 43132771 |
ls -li or statA link is simply an additional directory entry for a file or directory. It is between the filename and the actual data stored on the filesystem. By creating links we can have multiple directory entries (names) for the same file or directory, allowing two or more names for the same thing.
A hard link is a directory entry that points directly to the inode of a file. It acts as a separate file and refers to the exact spot on a hard drive. It is a mirror copy of the original file. Let's look at how to create hard links and find out how they work. To create the hard link, use ln command.
Creating a hard link:
Create an original file:
echo "Hello World!" > original.txt

Create a hard link:
ln original.txt hardlink.txt

Verify the inode numbers of the original and hard link files:
ls -i original.txt hardlink.txt

As we can see, both original.txt and hardlink.txt share the same inode number, indicating they point to the same file content.
Editing the hard link
Make changes to the hard link hardlink.txt:
echo "Nice to see you World" >> hardlink.txt
Check the content of the original file original.txt
cat original.txt

As we can see, changes made to hardlink.txt are reflected in original.txt because they refer to the same inode.
Deleting the original file
Delete the original file:
rm original.txt
Check the content of the hard link hardlink.txt:
cat hardlink.txt

As we can see, the file content is still accessible through hardlink.txt since the inode and data blocks are preserved until all hard links are removed.
Creating multiple hard links
Create another hard link:
ln hardlink.txt another-hardlink.txt
Verify Inode numbers:
ls -li hardlink.txt another-hardlink.txt

Again, both hardlink.txt and another-hardlink.txt share the same inode number, indicating they point to the same file content.
Hard links have the following advantages:
Efficient Storage: Hard links share the same inode and data blocks, which means no additional disk space is used for multiple hard links to the same file.
File Availability: As long as at least one hard link exists, the file's data remains accessible. Deleting one hard link does not delete the file.
Performance: Hard links are typically faster than symbolic links because they point directly to the inode without the need for path resolution.
Transparency: Hard links appear and behave like regular files. There is no distinction between the original file and its hard links.
Consistency: All hard links to a file are always up-to-date. Any changes made to the file through any hard link are immediately reflected in all other hard links.
While useful, there are some limitations to what hard links can do.
Restrictions on Directories: Hard links can only be created for regular files (not directories or special files).
Single Filesystem Limitation: Hard links cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.
A soft link (symbolic link) is a separate file that contains the path to another file or directory. Unlike hard links, soft links do not reference the inode of the target file directly but instead have their own inodes and contain the path to the target file or directory.
Create an original file:
echo "Hello World" > original.txt
Create a soft link using the command ln -s:
ln -s original.txt softlink.txt
Verify the soft link by typing:
ls -l original.txt softlink.txt

Read the content of the softlink.txt file:
cat softlink.txt

Edit the soft link softlink.txt:
echo "Nice to see you World!" >> softlink.txt
Check the content of the original.txt file:
cat original.txt

As we can notice, changes made through softlink.txt are reflected in original.txt.
Moving the target file
Move (or delete) the original.txt file to another location in the system:
mv original.txt ./backup/
Again, check the content of the softlink.txt file:
cat softlink.txt

As we can see, the soft link becomes a "dangling" link, as its target is moved to another location (or no longer exists in case it is deleted).
Because the soft link connection is a logical connection, not a duplication, soft links can point to entire directories or links to files on remote computers. Hard links cannot do this. Let's find out how it works.
Create a folder and a file:
mkdir my-folder
echo "Learn how to create soft links to folders" > my-folder/file
Create a soft link named soft-folder to the directory my-folder:
ln -s my-folder soft-folder
Verify the link:
ls -l
Access the directory my-folder using soft link soft-folder:
ls soft-folder
Delete the original folder my-folder and list the soft-folder:
rm -r my-folder
ls soft-folder

As we can see, since the original folder was removed, listing soft-folder shows nothing in the output. The target was deleted and the link became useless.
Soft links have the following advantages:
Flexibility: Soft links can link to both files and directories and span across different filesystems or partitions.
Convenience: It is easy to create shortcuts and organize files with soft links. They can link to non-existent files, which will work once the target is created.
Low Overhead: The size of soft link files is small since they only store the path to the target.
While useful, there are some drawbacks of soft links.
Fragility: They can become "dangling" links if the target is moved, renamed, or deleted.
Performance: They have slightly slower access than hard links because they require an extra path resolution step.
Security: They can potentially introduce security risks if not managed properly, as they can point to sensitive files.
Different Inodes: Soft links have their own inodes, which can add overhead to inode usage.