Hardlinks and Softlinks in Linux Explained With ExamplesLinux has the capability to create Softlinks and Hardlinks to files, similar to shortcuts in Windows, only they work on filesystem level.
This article is part of a series of articles. Please use the links below to navigate between the articles.
- How to Download and Installing Linux Step by Step For Beginners
- Essential Guide to Working with Files in Linux
- Understanding Linux File Permissions and Permission Calculator
- How to Archive, Compress and Extract Files in Linux
- Linux Piping and Redirection Explained
- Hardlinks and Softlinks in Linux Explained With Examples
- How to Create and Use Bash Scripts in Linux
- Data Recovery in Linux - How To Recover Your Data after Drive Failures
- Apache Web Server Administration Cheat Sheet for Linux
- Essential MariaDB and MySql Administration Tips on Linux
- How to Switching from Windows to Linux - A Complete Guide

In Linux, links come in hard and soft versions, referencing different addresses, each serving a different purpose.
A hard link is a reference directly to the location in the file system through an inode entry. A soft link, however, is a file with information pointing to another file; the linked file contains the reference to the inode. An inode is an inode table entry containing information (the metadata) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4. An inode number is also called an index number.
What are Linux Hardlinks
In this example, I will create a file called f1 and put "Hello" into it as its contents. I will then create a hard link to that file with the name f2.
echo hello > f1
ln f1 f2
ls -li f1 f2
As mentioned above, a hard link is directly to the inode entry. We can see this by comparing the entries on the ls command.
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f1
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f2
We can also look at the file contents and see they are identical.
cat f1 hello cat f2 hello
From this, we can see that the files f1 and f2 are the same; f2 points to f1.
Linux Softlink Examples
In this example, I will create a soft link to f1 called f3. I'll then list the inode entries.
ln -s f1 f3
ls -li f1 f2 f3
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f1
9514545 -rw-rw-r--. 2 root root 6 Dec 27 20:48 f2
9514546 lrw-rw-r--. 1 root root 6 Dec 27 20:48 f3 -> f1
We can see that the new soft link, f3, has a different inode number and a file type of link. It is a new, distinct file and points to f1. This can be seen from the f3 -> f1 indicator in the listing.
If we look at the file contents of f3, we can see that the soft link performs the same as the hard link.
cat f3 hello
Differences between Hardlinks and Softlinks
We can see that hard and soft links create links to files within the file system, and the links behave the same way, but when should you use a hard link?
Softlinks can be split across file systems (partitions), whereas hard links are only in the same file system. This is because inode 10000 on one partition references a different sector from inode 10000 on another.
If the original file or hard link is removed or changes locations, soft links will no longer work (the referenced file cannot be found)
Only Softlinks can link to a directory.
Hard links do not take up storage space, while the soft link will use up the minimal block size (typically 4k)
Hard Link Count and Sub Directory Count
The listing command, ls, shows the number of hard links next to the permissions block when viewing directories. This number represents the number of hard links below that directory. This example shows 110 "names" linked to this inode in the metadata. Metadata is the extra information the operating system holds on a file.
ls -ld /etc
drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc
Using the -i parameter with the ls command, we can see the inode number for an entry.
ls -ldi /etc
9388737 drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc
Every directory has a file with the name simply a dot. The . file represents the current directory, all directories have a . file, which will always point to the same inode entry.
ls -ldi /etc/.
9388737 drwxr-xr-x. 110 root root 8192 May 14 12:11 /etc/.
Every directory also contains a double dot file (..) This file is linked to the parent directory in which the current directory resides.
These dotfiles are included in the inode count, so from the initial inode count of 110, the number of subdirectories is 108. From this, we can say that the /etc/ directory has 108 sub-directories.