ln Command: Explaining Soft Links And Hard Links in Linux

Today we’re going to learn the difference between Hard links and Soft links on a Linux (ln command) Operating System.

Before understanding what is Hard links and Soft links, we need to understand the concept of inode.

What is inode in Linux file system

Inode is basically a pointer of a file on the Hard disk.

If I create a file called file.txt. that file name is for me as a Human to understand the name of the file.

But computer doesn’t understand names, computer understand numbers.

Every time we try to retrieve that file or read that file it goes and retrieve that inode number.

So for every file in our file system there exists an Index Node or an inode and inode is like a database of a file.

It contains many information about the file however it does not contain two important things.

Inode does not contain the file content and it does not contain the file name.

The inode contains a list of pointers to the disk blocks that belong to that file or directory. The disk blocks store the data for the inode.

So, you can think of an inode like a Personal ID or a Passport but without your name on it.

inode is nothing but one data structure which will having one header containing plenty such information and thirteen fields are there which will be containing some direct single into a double indirect and trivial indirect block addresses.

So what are the different fields are there existing in the header part of the inode. Let me discuss it one by one.

Typically a inode header contains the following important information’s related to a file:

  • Inode number
  • File size(In Bytes)
  • Owner Information
  • Owning Group Information
  • File Type (File/Directory/Link)
  • Permissions
  • Number of Links
  • Protection bits
  • File creation/access/modification times

You can use the ls command to check the inode number of a file or directory.

# ls -li
total 0
52351765 -rw-r--r--. 1 root root 0 Mar 25 06:01 file5.txt
52351767 -rw-r--r--. 1 root root 0 Mar 25 06:01 file6.txt
34548616 drwxr-xr-x. 2 root root 6 Mar 25 06:00 mydata

Read Also: ls Command Examples in Linux

So let’s get to the topic.

Difference between Hard links and Soft links

Soft links are often also called Symbolic links but either way is fine.

Either soft or symbolic both are correct ways to reference symbolic or soft links.

So, what are we talking about, what’s the difference between a Hard link and a Soft link.

Well it comes down to what they are referencing.

So, a Hard link is basically a file and it references or points to a spot on a harddrive.

So, this down here is my representation of a hard drive actually the inode layout on a hard drive, how the hard drive stores data.

Hardlinks and Softlinks
Hardlinks and Softlinks

Explaining Hard Link

So file1.txt here points to a specific spot on the hard drive and that’s where it stores its data.

If you create a hard-linked file like name it file2.txt for example, it’s pointing to the exact same spot on the hard drive.

Now what this effectively gives you is two separate files that behave just like separate files with the exception that if you make an edit to file1.txt, file2.txt will automatically be edited because they’re pointing to that same spot on the hard drive.

If you change the data down here that they’re both pointing to they’re both going to change that’s just how hard links work.

Note: Hard links are unable to cross different file systems.

Explaining Soft Link

Soft links or Symbolic links on the other hand work differently.

If you are familiar with Microsoft Windows, you will know what is a shortcut and basically a Soft link is the same like a shortcut in Windows.

Now the original file file1.txt in our case does the same thing it points to a spot on the hard drive and that’s where it stores its data.

When you create a symbolic link however instead of another file that points to the same spot on the hard drive, this file actually just points to the file1.txt descriptor or the file1.txt name.

It points to the name of the file instead of pointing to a spot on the hard drive.

So, what does that mean?

Well it means that file2.txt takes up almost no space because all it is like a pointer going to that file.

So even if this is like a 5 Terabyte file, if you create a symbolic link it’s just going to be pointing at that and be very small.

However, it means if you delete file1.txt, file2.txt will be completely useless because it’s going to be pointing at something that doesn’t exist.

In case of Hard link if you delete the original file, in our case it’s file1.txt, file2.txt will be still perfectly viable because it’s pointing to the spot on the hard drive where the data is stored.

So, you can delete one or the other of these hard-linked files and the data is still intact and it’s still there.

But with the symbolic link if you delete the original file the symbolic link is useless.

Note: Soft links can be linked across different file systems.

So, let’s go to the command line and see what that looks like in practice because it’s not terribly confusing once you understand what’s actually going on.

So, here I am on my Ubuntu system and I’ve opened up a terminal window.

Now if I type ls command you’ll see that there’s a file called file1.txt.

# ls
file1.txt

If we say cat it will show us the contents of this file1.txt and basically, it’s just a text file that says this is a regular file.

# cat file1.txt 
I love Linux.
Linux is a Open Source Operating System.

Read Also: cat Command Examples in Linux

How to create Hard links in Linux

You must follow the syntax given below while creating Hard link using ln command.

ln [OPTIONS] FILE LINK

So, let’s start out by creating a hard link to file1.txt and to do that you use the ln command with no flags or anything just ln.

The source which is file1.txt and what we want to call the second hard linked file, let’s call it file2.txt.

# ln file1.txt file2.txt

If we type ls -l now, you’ll see we have two files in this directory.

# ls -l
-rw-r--r--. 2 root root 55 Mar 24 08:19 file1.txt
-rw-r--r--. 2 root root 55 Mar 24 08:19 file2.txt

We have our original file file1.txt which is 55 bytes long we also have a new file that behaves and looks exactly like the file1.txt, it’s named file2.txt and it also has 55 bytes.

And as you can see that file2.txt file also has the same content as file1.txt file.

# cat file2.txt 
I love Linux.
Linux is a Open Source Operating System.

Why does it also have the exact same content and bytes because it’s pointing to the same spot on the hard drive.

Now let’s see how that works in practice.

Let’s edit file2.txt and add some content to it. You can use any text editor to do this. I would like to use nano text editor.

# nano file2.txt
I love Linux.
Linux is a Open Source Operating System.
Linux is OS or Kernel

If we display the contents of our second file i.e. file1.txt under the cat command, then as you can see that the same data is visible to us in this file as well.

# cat file1.txt 
I love Linux.
Linux is a Open Source Operating System.
Linux is OS or Kernel

Like I told you that both files are pointed to the same spot on the hard disk.

So as soon as I saved a file by making some changes, it was reflected in the other file as well.

The Hard link’s inode number is the same as the inode number of the original file.

# ls -li
2429406 -rw-r--r--. 2 root root 0 Mar 25 06:42 file1.txt
2429406 -rw-r--r--. 2 root root 0 Mar 25 06:42 file2.txt

Now the cool news is if we delete file1.txt (our original file) we can still look at the contents of file2.txt and of course it’s still there because it’s pointing to that spot in the hard drive where the data is stored.

# rm file1.txt 
rm: remove regular file 'file1.txt'? y

# cat file2.txt 
I love Linux.
Linux is a Open Source Operating System.
Linux is OS or Kernel

How to create Soft links in Linux

You must follow the syntax given below while creating Soft link using ln command.

ln -s [OPTIONS] FILE LINK

Now Soft links or Symbolic links work differently. I’ll show you how that works.

So here we have file2.txt.

# ls
file2.txt

How do I create a Symlink to a file in Linux?

To create a Soft link or a Symbolic link to that file, use the same ln command but now you use the -s for symbolic and then you say the source file which in our case is now file2.txt and then mention a name for the Soft link which in our case softlink.txt

# ln -s file2.txt softlink.txt

If we type ls -l to see what’s going on and we get this really nice display by ls.

# ls -l
-rw-r--r--. 1 root root 77 Mar 24 23:35 file2.txt
lrwxrwxrwx. 1 root root  9 Mar 25 00:00 softlink.txt -> file2.txt

So our original file file2.txt is still pointing to the hard drive where the data on the hard drive is stored, however softlink.txt is not pointing to the hard drive it’s just pointing to the file name file2.txt.

Now if we cat soft link it’s going to show us the contents of that file because it’s soft linked to file2.txt.

# cat softlink.txt 
I love Linux.
Linux is a Open Source Operating System.
Linux is OS or Kernel

You have to know one more thing that an inode number of a Soft link is different than the inode number of the original file.

Here is an example.

# ls -li
52351765 -rw-r--r--. 1 root root 0 Mar 25 06:23 file2.txt
52351767 lrwxrwxrwx. 1 root root 9 Mar 25 06:24 softlink.txt -> file2.txt

The problem comes if we remove file2.txt

# rm file2.txt 
rm: remove regular file 'file2.txt'? y

Now if we type ls -l it gives us red errors here.

Broken Softlink
Broken Softlink

It says ok softlink.txt is pointing to the file file2.txt, but that doesn’t exist and now this Soft link is a broken link.

If we try to cat it now it’s going to say, there’s no such file or directory.

# cat softlink.txt 
cat: softlink.txt: No such file or directory

What it’s talking about is there’s no file or directory that it’s trying to point. file2.txt is gone so that data that we had there.

Since the soft link doesn’t point to the hard drive location it just points to a filename, we’ve lost that data.

So Hardlinks are more forgiving when you delete a file.

Soft links take up less data because it’s just a pointer however Soft links don’t store the actual data they just stored the location of the original file where it was stored on.

How do I create a Symlink to a directory in Linux?

Type the following command to create a symlink to a directory.

In this example I am creating a Soft link called my_documents for the /root/mydata/important-documents/files directory.

# ln -s /root/mydata/important-documents/files my_documents

How do I remove a Symbolic link?

There are two ways you can remove Symlink.

Either you remove the Symlink using the rm command or you can also use the unlink command for this task.

Syntax:

rm LINK

OR

unlink LINK

Removing the Symlink using the rm command.

# rm softlink.txt
rm: remove symbolic link 'softlink.txt'? y

Removing the Symlink using the unlink command.

# unlink softlink.txt

How to Overwrite symbolic links

To overwrite an existing symbolic link use the -f option with the ln command.

# ln -sf file2.txt softlink.txt

You will get this error if you try to overwrite the existing Sym link without using the -f option.

# ln -s file2.txt softlink.txt
ln: failed to create symbolic link 'softlink.txt': File exists

You can visit at following websites to get more information on ln command.

Conclusion

I hope that now you have a good understanding of how Soft Link And Hard Link works in Linux and you have some ideas for how you can use ln command within your workflow.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

Leave a Comment