Hard link and Soft link (Symbolic link) in Linux are two different methods to refer to a file in the hard drive.

Inode

Each file has one inode that contains information about that file, including the location of the data belonging to that file. Every file must have at least one directory entry that points to the inode that describes the file. The directory entry is a hard link, thus every file has at least one hard link. It is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive. It also has information about the permission privileges.

ln Command

ln command is used to create link between files. By default, it creates a hard links. To create a symbolic link, use the -s option. Syntax of the command is

ln -s [OPTIONS] FILE LINK

If both the FILE and LINK are given, ln will create a link from the file specified as the first argument (FILE) to the file specified as the second argument (LINK). To overwrite the destination path of the symlink, use the -f (–force) option.

A hard link is essentially a synced carbon copy of a file. It is a direct reference to a file via its inode. By using a hardlink, you can change the original file’s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file.

Following example shows how to create hard link. Lets first create a directory and a file inside it.

$ mkdir LinkTest
$ cd LinkTest
$ touch test.txt

Now, create a hard link to the file. Below command will create hard link linked_file to the file test.txt.

$ ln test.txt linked_file

To display the inodes for both files, pass i argument to the ls command. Output of the command is

root@tryit-cunning:~/LinkTest# ls -il test.txt linked_file                                                                                         
20624 -rw-r--r-- 2 root root 0 Jan 17 12:02 linked_file                                                                                            
20624 -rw-r--r-- 2 root root 0 Jan 17 12:02 test.txt 

From the output, you can notice that both test.txt and linked_file have the same inode number (20624). Also, both files have the same file permissions and the same size.

Now Remove the original file test.txt.

$ rm test.txt

After removing hard link, content of linked_file is same as that of the original file. To view the content of file execute

$ cat linked_file

So a hard Link

  • can’t cross the file system boundaries,
  • can’t link directories,
  • has the same inodes number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

Soft Link or Symbolic links are essentially shortcuts that reference to a file instead of its inode value. Since the symbolic link is referring to the original file and not its inode value, then replacing the original file into a different folder will break the symbolic link, or create a dangling link.

Following example shows how to create soft link. To create soft link use -s option in ln command. Below command will create soft link linked_soft to file test.txt.

$ ln -s test.txt linked_soft

To display inodes for both file, use pass i argument to ls command as shown below. Output of the command is

Soft link in Linux

From the output, you can notice that the inodes are different and the symbolic link has an “l” before the rwxrwxrwx. The permissions are different for the link and the original file because it is just a symbolic link.

Now remove the original file (test.txt), but if you try to view the content, it will throw error.

root@tryit-cunning:~/LinkTest# rm test.txt
root@tryit-cunning:~/LinkTest# cat linked_soft
cat: linked_soft: No such file or directory

In a nutshell, a soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inodes number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

To delete/remove symbolic links use either the unlink or rm command.

unlink symlink_to_remove

Removing a symbolic link using the rm command is the same as when removing a file:

rm symlink_to_remove