Create links with ln command

Share this post to your friends !

This entry is part 4 of 6 in the series Linux commands: Part 3 - Exploring the system
Create links with ln command
5 (100%) 1 vote[s]

WriteBash - What is the ln command used for? The ln command in linux is used to create a hard link or symbolic link. In this article, we will also learn what hard link is and what the symbolic link is. We will also learn how to use the ln command to create a link.

What does the ln command do?

As noted at the beginning of this article, ln command in Linux uses to create a hard link or symbolic link.

So why do we have to make a link, this link is used for what purpose. If you have ever worked with a web server then you will know the link. As you can imagine, we have a directory containing configuration files for editing (I called folder A), we have another directory to contain the configuration files for the web server executable (i called folder B). We make an edit in folder A and then create a link to folder B. Thus, the web server can read the changes without affecting the original configuration file.

How to use the ln command

The use of ln command is very simple, there are two cases:

– Create a hard link:

ln file link

– Create a symbolic link:

ln -s item link

What is a hard links?

Hard links are links in the same file system with two corresponding entry nodes pointing to the same physical content.

create-links-with-ln-command-01 Create links with ln command
Picture about creating a hard link in Linux.

You can see the image above for an example of how to create a hard link. Below I will explain the 7 steps in the image above.

1. For example, I would create a root file in the home directory named writebash.txt.

echo "WriteBash.com" > /home/writebash.txt

2. Check the file you just created will see the link count is 1.

ls -l /home/writebash.txt

3. In this step, we will create a hard link to the file just created.

ln /home/writebash.txt /home/writebash_hardlink.txt

4. We check the newly linked file we will see the link count increase to 2.

ls -l /home/writebash_hardlink.txt

5. When we check the inode of both files, we find that the inode of the two files is exactly the same, and the link count is 2.

ls -li /home/writebash*

6. Now, we delete the original file.

rm -f /home/writebash.txt

7. Check the linked file, we see that the linked file is not lost because the system only deletes the link count in the inode for 1.

ls -li /home/writebash_hardlink.txt

Note:

  • Hard links can not be created on two different partitions. Ie the original file and the file pointing to the original file must be the same partition.
  • It is not possible to create a hard link to a directory.

What is a symbolic link?

A symbolic link is an unused link to a node entry that is merely a shortcut. It will create a new inode and the contents of this inode will point to the inode of the original file.

Read more: Copy files and directories with ‘cp’ command.

create-links-with-ln-command-02 Create links with ln command
Picture about creating a soft link in Linux.

Unlike the hard link above, you can see in the image above there are some differences.

1. When you add content to a linked file, the content is added to the original file.

2. The inode of the two files is different and the link count is just 1.

3. When you delete the original file, the content in the linked file does not exist anymore. That’s because the link file is pointing to an original file and when the original file no longer exists cause it also can not be displayed.

Note:

  • Symbolic link can link to a directory.
  • Symbolic link can link to two files belonging to two different partitions.

Conclusion

By this post, hope you can understand what is the link in linux. Understand how to use the ln command to create links in linux, applying each case to a hard link or symbolic link.

Continue reading the series«« Previous part: Use less command to view long text documentsNext part: Understand long format of ls command in Linux »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.