How to Create Symbolic and Hard Links in Linux
On a Linux system, you can create links to a file, which act like shortcuts or references to the original file. There are two types of links: hard links and symbolic (soft) links. You can create multiple links to the same file, making it easier to access or organize your files. Let’s take a quick look at both types and how they differ.
Hard Links
In Unix-based or Linux-based systems, a file consists of data blocks and an inode. The data blocks hold the file’s actual content, while the inode stores its attributes (except the name) and the locations of those blocks.
A hard link is simply another file that points to the same inode as the original. This means both files share the same physical data on disk.
You can use the ln
command to make a hard link.
# ls -l
# touch originalfile
# ls -l
# ln originalfile samplehardlinkfile
# ls -l

By default, the ln
command makes hard links. Let’s check their inode numbers real quick:
# ls -i -1

Both files link to the same inode, so even if we delete the original, we can still access the content through the hard link. A file is only truly deleted when no hard links point to it.
The -1
option in the command lists files one per line. Instead of copying, we can use cp -l
to create hard links.
However, we can’t create hard links for directories, and they don’t work across different filesystems, like network-mounted drives.
Symbolic Links
A soft link is like a shortcut—it points to the original file’s location instead of storing its actual content. If you move or delete the original file, the link stops working.
Let’s create a soft (symbolic) link now.
# ln -s originalfile originalfilesoftlink
# ls -l

The -s
option (short for --symbolic
) creates a symbolic link to a file.
Unlike hard links, a symbolic (or soft) link is just a pointer to the original file and has a different inode number.
# ls -i -1

You can create a soft link to a directory, allowing you to connect files across different filesystems. To make a soft link, you can use the cp -s
command, where -s
stands for --symbolic-link
.
Now that we get what soft and hard links are, let’s quickly break down the key differences:
- Hard links act like copies of the original file, sharing the same inode number.
- Soft links are separate files that just point to the original file’s location.
- Hard links still work even if the original file is moved or deleted.
- Soft links break if the original file is moved or deleted.
- You can create soft links for directories, but not hard links.
- Soft links can work across different filesystems, unlike hard links.