Create links with ln command

by Daniel Pham
Published: Updated:
This entry is part 4 of 6 in the series Linux commands: Part 3 - Exploring the system

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
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
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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Use less command to view long text documentsNext part: Understand long format of ls command in Linux »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.