What are symlinks and how to create them in Linux?


Symbolic links also known as symlinks or soft links are special types of files that point to another file or directory in the filesystem. These are very much similar to the shortcuts used in Windows.

Symlinks are very useful these have the ability to create links to file or directories on a remote computer connected using NFS. The hard links do not possess such capabilities.

Here we will discuss how to create a symlink on Linux or Unix system.

Types of links in Linux or Unix

In general terms, a link basically represents the connections between two things similarly in Linux a link is a pointer that points to a file or directory.

In Linux, links can be categorized into two types –

1. Hard link –

If you create a hard link to a file or directory the created link will be the same as the original one. Each hard link shares the same index node value. A hard link retains its value after deleting the original file or directory.

2. Soft link –

A soft link is very much like a shortcut of a file or directory in the file system. Symlinks are dangling when you delete the original file the all symlinks created to that file will be broken.

For instance, suppose you have created a hard link name test_hlink and a soft link called test_slink to testfile.txt file. Now when you delete testfile.txt the content of test_hlink will still exist and when you click on test_slink it will display a message saying the link is broken.

How to create a hard link using the ln command

Linux or Unix provides ln command to create a hard or soft link by default it creates a hard link. The syntax of the ln command is given below –

ln [Option][Orignal file or directory name][Link name]

To create a hard link use the following command-

ln source_file_or_directory link_name

For example –

ln testfile.txt test_hlink

A hard link with the name test_hlink will be created to the file testfile.txt.

How to create a symbolic link using the ln command

For creating a symbolic link to the source file you need to use -s option with the ln command. The syntax and example are given below-

ln -s source_file_or_directory link_name

For example –

ln -s testfile.txt test_slink

Once this command executes a shortcut or link of testfile.txt will be created now see the image below.

If you want to know more about the usage of the ln command then see its manual page in your terminal by using –

man ln

Conclusion

Now you know what is a symlink or hard link and how to create them. If you have a query then leave it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.