How to Create hard links and symbolic (or "soft") links in Linux
It is possible to create multiple names that point to the same file. There are two ways to do this: by creating a hard link to the file, or by creating a soft link (sometimes called a symbolic link) to the file. Each has its advantages and disadvantages.
Creating Hard Links
Every file starts with a single hard link, from its initial name to the data on the file system. When you create a new hard link to a file, you create another name that points to that same data. The new hard link acts exactly like the original file name. Once created, you cannot tell the difference between the new hard link and the original name of the file.
You can find out if a file has multiple hard links with the ls -l command. One of the things it reports is each file’s link count, the number of hard links the file has.
[user@host ~]$ pwd
/home/user
[user@host ~]$ 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 file. The command needs at least two arguments, a path to the existing file, 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 file newfile.txt in the /tmp directory.
[user@host ~]$ ln newfile.txt /tmp/newfile-hlink2.txt
[user@host ~]$ 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 find out whether two files are hard links of each other, one way is to use the -i option with the ls command to list the files’ inode number. If the files are on the same file system (discussed in a moment) and their inode numbers are the same, the files are hard links pointing to the same data.
[user@host ~]$ 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 file gets deleted, the contents of the file 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.
[user@host ~]$ rm -f newfile.txt
[user@host ~]$ ls -l /tmp/newfile-hlink2.txt
-rw-rw-r--. 1 user user 12 Mar 11 19:19 /tmp/newfile-hlink2.txt
[user@host ~]$ 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 files. You cannot use ln to create a hard link to a directory or special file.
Secondly, hard links can only be used if both files are on the same file system. The file-system hierarchy can be made up of multiple storage devices. Depending on the configuration of your system, when you change into a new directory, that directory and its contents may be stored on a different file system.
You can use the df command to list the directories that are on different file systems. For example, you might see output like the following:
[user@host ~]$ 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
[user@host ~]$
Files in two different “Mounted on” directories and their subdirectories are on different file systems. (The most specific 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 first file is in a subdirectory of /boot (on the “Mounted on” list) and the second file 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 file, but a special type of file that points to an existing file or directory.
Soft links have some advantages over hard links:
- They can link two files on different file systems.
- They can point to a directory or special file, not just a regular file.
In the following example, the ln -s command is used to create a new soft link for the existing file /home/user/newfile-link2.txt that will be named /tmp/newfile-symlink.txt.
[user@host ~]$ ln -s /home/user/newfile-link2.txt /tmp/newfile-symlink.txt
[user@host ~]$ 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
[user@host ~]$ cat /tmp/newfile-symlink.txt
Soft Hello World
In the preceding example, the first character of the long listing for /tmp/newfile-symlink.txt is l instead of -. This indicates that the file is a soft link and not a regular file. (A d would indicate that the file is a directory.) When the original regular file gets deleted, the soft link will still point to the file but the target is gone. A soft link pointing to a missing file is called a “dangling soft link.”
[user@host ~]$ rm -f newfile-link2.txt
[user@host ~]$ ls -l /tmp/newfile-symlink.txt
lrwxrwxrwx. 1 user user 11 Mar 11 20:59 /tmp/newfile-symlink.txt -> /home/user/newfile-link2.txt
[user@host ~]$ 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.
[user@host ~]$ ln -s /etc /home/user/configfiles
[user@host ~]$ cd /home/user/configfiles
[user@host configfiles]$ pwd
/home/user/configfiles