How to create a Symbolic link in Linux?

Gaurav Bhardwaj
Gaurav Bhardwaj
Published on
#linux
Cover Image for How to create a Symbolic link in Linux?

I see many people wonder how to create a symbolic link in Linux, so I thought to create an article on this topic. Symbolic links also termed soft links are very useful in Linux. A symbolic link is a special kind of file in Linux that points to another file. It’s just like a shortcut if you are coming from Microsoft Windows or an alias if you are coming from Macintosh OS. In this article, we will learn how to create a symbolic link in Linux.

Lets begin with How to create a Symbolic link in Linux:

the command ln is used to make the link between the files. There are two types of links one is the soft link or symbolic link another one is the hard link. If we specify -s then it means that it is a soft link or symbolic link.

The usage of ln command is as follows:

ln -s <Source Directory> <Symbolic Link>
How to create a Symbolic link in Linux

Suppose we have a directory in our home directory – say, Dir and we have further two directories inside that, one is A, other is B. Inside A directory there are further two directories i.e C and D. The B directory has one file inside it say, abc.txt. Now let’s create a symbolic link between directory D and B.

We can easily create a symbolic link with the below command.

ln -s A/D B

A symbolic link will be created from the D directory to the B Directory.


How to create a Symbolic link in Linux

Now If you see the contents of the B Directory it has a symbolic link of D Directory.

How to create a Symbolic link in Linux

If you see the contents of the B Directory you will see the soft link represented by D -> A/D

How to create a Symbolic link in Linux

Below is the output of the ls --recursive which states that due to symbolic link you have a directory D present inside the B directory but not vice-versa.



How to create a Symbolic link in Linux

If you delete the D directory, the parent directory will not be deleted since it is a symbolic link only.

Now, Let’s take one another example of creating the files and editing it via symbolic links.

Lets create a directory inside the home directory named as File & create a file inside that hello.txt

How to create a Symbolic link in Linux

Further let’s add some content inside the hello.txt file.

How to create a Symbolic link in Linux

Time to create a symbolic link of File/hello.txt as hi.txt in home directory.

ln -s File/hello.txt hi.txt
How to create a Symbolic link in Linux

Since we have created a symbolic link, now lets see the contents of hi.txt

How to create a Symbolic link in Linux

As expected, we have the same output hi.txt as of hello.txt since the symbolic link is created. Further, we will try to change the contents of hi.txt and see whether the contents of hello.txt is changed or not.

How to create a Symbolic link in Linux

Now lets see the contents of hello.txt

How to create a Symbolic link in Linux

Now lets try to delete the hi.txt file and again check if there is any impact of the same on hello.txt or not.

How to create a Symbolic link in Linux

There is not any impact of deleting the file, So there is one conclusion if you delete a file, only a symbolic link file will be deleted the original file will stand unchanged and if you write a text inside the symbolic link, the original file will also get changed. That’s it about symbolic links.

There are lot more things we can do with symbolic links, I mostly install my own software tarballs from the web & then utilize the symbolic links for creating links to directly executing them. I will cover that in the next article. If you have any queries or suggestions on this article, please write in the comment section below.

Also if you are interested in learning how to set the Linux environment variables permanently you can visit this article.


0 Comments

Leave a Comment

Note : Your Email ID will not be published. Enter all the Required Fields *