avatarYang Zhou

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1636

Abstract

r example, there is a file called <code>test.txt</code> on my system and I created a hard link for it called <code>test2.txt</code>:</p><div id="e00b"><pre><span class="hljs-built_in">ln</span> test.txt test2.txt</pre></div><p id="6046">Then, we can use the <code>ls -i</code> command to check their inode numbers:</p><figure id="4451"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*npOQ5ZL-0065RJFePqtC3A.png"><figcaption></figcaption></figure><p id="07fa">As shown above, the <code>test2.txt</code> has the same inode number with the <code>test.txt</code>.</p><p id="f2f3">Hard links, by the way, cannot be created for directories and files on a different filesystem, cause different filesystems manage their own inodes.</p><h1 id="91b8">Soft/Symbolic Links</h1><p id="ce1a">A soft link is a pointer to a file or directory.</p><p id="37f2">Unlike a hard link, a soft link has its own inode and inode number. But it points to another existing file’s content.</p><p id="e365">This means that a soft link depends on its “host file” to exist. If its host file is deleted, opening the soft link will report an error: “No such file or directory”. (This is totally different with hard links.)</p><h2 id="ebeb">How to create a soft link</h2><p id="d183">We can create a soft link by the <code>ln</code> command with the help of a <code>-s</code> option.</p><div id="bbc3"><pre><span class="hljs-built_in">ln</span> -s test.txt test3.txt</pre></div><p id="0bd3">The above command creates a soft link for my file <code>test.txt</code>.</p><p id="029b">Now, my original file <code>test.txt</code> has a hard link and a soft li

Options

nk.</p><p id="8965">Let’s input the <code>ls -li</code> command to check out the details of it:</p><figure id="f15f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xBZrPmGaSuTu5_lGLBE-sQ.png"><figcaption></figcaption></figure><p id="aee7">As shown above, the <code>test.txt</code> and <code>test2.txt</code> have the same inode number(8800836), but the inode number of the <code>test3.txt</code> is 8800838. And obviously, the results told us it’s a soft link by displaying it as <code>test3.txt->test.txt</code>.</p><p id="ac01">Since it’s a pointer and there’s nothing about inodes, a soft link can point to a file or a directory on a different filesystem or partition.</p><p id="373c"><b><i>Thanks for reading. If you like it, please follow <a href="https://yangzhou1993.medium.com/follow">me</a> and become a <a href="https://yangzhou1993.medium.com/membership">Medium member</a> to enjoy more great articles. </i></b>🙂</p><p id="eb4e">Relative articles:</p><div id="44da" class="link-block"> <a href="https://readmedium.com/inodes-a-starting-point-for-understanding-linux-filesystems-e153a905d7e"> <div> <div> <h2>Inodes: A Starting Point for Understanding Linux Filesystems</h2> <div><h3>From concepts to practical usages</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Wxe8_Cwooyq0oY2xuvgw3A.png)"></div> </div> </div> </a> </div></article></body>

Hard Links VS Soft Links in Linux: What’s the Difference

From concepts to operations

Image from HAkawa on Wallhaven

If you are a backend or DevOps developer, there are two special concepts you’re supposed to know of Linux/UNIX filesystems: hard links and soft links (or called symbolic links). Because sometimes it may lead to wrong operations for files on a Linux system if you don’t know the difference between them.

Hard Links

A Linux system uses an inode to store the metadata of a file and every inode has an inode number as its identifier. It’s possible that multiple filenames point to the same inode number.

This mechanism has advantages:

  • A file can be accessed with different filenames.
  • Modifications of a file by one of its filenames will affect all filenames.
  • Deleting one filename does not affect access to other filenames.

Therefore, it’s clear what a hard link is. A hard link is a link pointing to an existing file’s inode. One or more hard links for an existing file can be created.

How to create a hard link

We can use the ln command to create a hard link.

For example, there is a file called test.txt on my system and I created a hard link for it called test2.txt:

ln test.txt test2.txt

Then, we can use the ls -i command to check their inode numbers:

As shown above, the test2.txt has the same inode number with the test.txt.

Hard links, by the way, cannot be created for directories and files on a different filesystem, cause different filesystems manage their own inodes.

Soft/Symbolic Links

A soft link is a pointer to a file or directory.

Unlike a hard link, a soft link has its own inode and inode number. But it points to another existing file’s content.

This means that a soft link depends on its “host file” to exist. If its host file is deleted, opening the soft link will report an error: “No such file or directory”. (This is totally different with hard links.)

How to create a soft link

We can create a soft link by the ln command with the help of a -s option.

ln -s test.txt test3.txt

The above command creates a soft link for my file test.txt.

Now, my original file test.txt has a hard link and a soft link.

Let’s input the ls -li command to check out the details of it:

As shown above, the test.txt and test2.txt have the same inode number(8800836), but the inode number of the test3.txt is 8800838. And obviously, the results told us it’s a soft link by displaying it as test3.txt->test.txt.

Since it’s a pointer and there’s nothing about inodes, a soft link can point to a file or a directory on a different filesystem or partition.

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles. 🙂

Relative articles:

Linux
Programming
DevOps
Software Development
Technology
Recommended from ReadMedium