How to Create hard links and symbolic (or "soft") links in Linux
It is possible to create multiple names that point to the same ﬁle. There are two ways to do this: by creating a hard link to the ﬁle, or by creating a soft link (sometimes called a symbolic link) to the ﬁle. Each has its advantages and disadvantages.
Creating Hard Links
Every ﬁle starts with a single hard link, from its initial name to the data on the ﬁle system. When you create a new hard link to a ﬁle, you create another name that points to that same data. The new hard link acts exactly like the original ﬁle name. Once created, you cannot tell the difference between the new hard link and the original name of the ﬁle.
You can ﬁnd out if a ﬁle has multiple hard links with the ls -l command. One of the things it reports is each ﬁle’s link count, the number of hard links the ﬁle has.
[[email protected] ~]$ pwd /home/user [[email protected] ~]$ ls -l newfile.txt -rw-r--r--. 1 user user 0 Mar 11 19:19 newfile.txt
In the preceding example, the link count of newfile.txt is 1. It has exactly one absolute path, which is /home/user/newfile.txt.
You can use the ln command to create a new hard link (another name) that points to an existing ﬁle. The command needs at least two arguments, a path to the existing ﬁle, and the path to the hard link that you want to create. The following example creates a hard link named newfile-link2.txt for the existing ﬁle newfile.txt in the /tmp directory.
[[email protected] ~]$ ln newfile.txt /tmp/newfile-hlink2.txt [[email protected] ~]$ ls -l newfile.txt /tmp/newfile-hlink2.txt -rw-rw-r--. 2 user user 12 Mar 11 19:19 newfile.txt -rw-rw-r--. 2 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt
If you want to ﬁnd out whether two ﬁles are hard links of each other, one way is to use the -i option with the ls command to list the ﬁles’ inode number. If the ﬁles are on the same ﬁle system (discussed in a moment) and their inode numbers are the same, the ﬁles are hard links pointing to the same data.
[[email protected] ~]$ ls -il newfile.txt /tmp/newfile-hlink2.txt 8924107 -rw-rw-r--. 2 user user 12 Mar 11 19:19 newfile.txt 8924107 -rw-rw-r--. 2 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt
Even if the original ﬁle gets deleted, the contents of the ﬁle are still available as long as at least one hard link exists. Data is only deleted from storage when the last hard link is deleted.
[[email protected] ~]$ rm -f newfile.txt [[email protected] ~]$ ls -l /tmp/newfile-hlink2.txt -rw-rw-r--. 1 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt [[email protected] ~]$ cat /tmp/newfile-hlink2.txt Hello World
Limitations of Hard Links
Hard links have some limitations. Firstly, hard links can only be used with regular ﬁles. You cannot use ln to create a hard link to a directory or special ﬁle.
Secondly, hard links can only be used if both ﬁles are on the same ﬁle system. The ﬁle-system hierarchy can be made up of multiple storage devices. Depending on the conﬁguration of your system, when you change into a new directory, that directory and its contents may be stored on a different ﬁle system.
You can use the df command to list the directories that are on different ﬁle systems. For example, you might see output like the following:
[[email protected] ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 886788 0 886788 0% /dev tmpfs 902108 0 902108 0% /dev/shm tmpfs 902108 8696 893412 1% /run tmpfs 902108 0 902108 0% /sys/fs/cgroup /dev/mapper/rhel_rhel8--root 10258432 1630460 8627972 16% / /dev/sda1 1038336 167128 871208 17% /boot tmpfs 180420 0 180420 0% /run/user/1000 [[email protected] ~]$
Files in two different “Mounted on” directories and their subdirectories are on different ﬁle systems. (The most speciﬁc match wins.) So, the system in this example, you can create a hard link between /var/tmp/link1 and /home/user/file because they are both subdirectories of / but not any other directory on the list. But you cannot create a hard link between /boot/ test/badlink and /home/user/file because the ﬁrst ﬁle is in a subdirectory of /boot (on the “Mounted on” list) and the second ﬁle is not.
Creating Soft Links
The ln -s command creates a soft link, which is also called a “symbolic link.” A soft link is not a regular ﬁle, but a special type of ﬁle that points to an existing ﬁle or directory.
Soft links have some advantages over hard links:
- They can link two ﬁles on different ﬁle systems.
- They can point to a directory or special ﬁle, not just a regular ﬁle.
In the following example, the ln -s command is used to create a new soft link for the existing ﬁle /home/user/newfile-link2.txt that will be named /tmp/newfile-symlink.txt.
[[email protected] ~]$ ln -s /home/user/newfile-link2.txt /tmp/newfile-symlink.txt [[email protected] ~]$ ls -l newfile-link2.txt /tmp/newfile-symlink.txt -rw-rw-r--. 1 user user 12 Mar 11 19:19 newfile-link2.txt lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfile-link2.txt [[email protected] ~]$ cat /tmp/newfile-symlink.txt Soft Hello World
In the preceding example, the ﬁrst character of the long listing for /tmp/newfile-symlink.txt is l instead of -. This indicates that the ﬁle is a soft link and not a regular ﬁle. (A d would indicate that the ﬁle is a directory.) When the original regular ﬁle gets deleted, the soft link will still point to the ﬁle but the target is gone. A soft link pointing to a missing ﬁle is called a “dangling soft link.”
[[email protected] ~]$ rm -f newfile-link2.txt [[email protected] ~]$ ls -l /tmp/newfile-symlink.txt lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfile-link2.txt [[email protected] ~]$ cat /tmp/newfile-symlink.txt cat: /tmp/newfile-symlink.txt: No such file or directory
One way to compare hard links and soft links that might help you understand how they work:
- A hard link points a name to data on a storage device
- A soft link points a name to another name, that points to data on a storage device
A soft link can point to a directory. The soft link then acts like a directory. Changing to the soft link with cd will make the current working directory the linked directory. Some tools may keep track of the fact that you followed a soft link to get there. For example, by default cd will update your current working directory using the name of the soft link rather than the name of the actual directory. (There is an option, -P, that will update it with the name of the actual directory instead.)
In the following example, a soft link named /home/user/configfiles is created that points to the /etc directory.
[[email protected] ~]$ ln -s /etc /home/user/configfiles [[email protected] ~]$ cd /home/user/configfiles [[email protected] configfiles]$ pwd /home/user/configfiles