Introduction

In this post, we covered file linking in Linux and demonstrated the difference between soft and hard links as well as between file linking and copying.

Skills Learned

File Linking in Linux

RHEL

File Attributes and Inodes

Before we get into linking, it’s important to understand a little bit about how Linux handles files. Every file has what we call metadata, which includes things like the file type, size, permissions, owner, and when it was last accessed or modified.

This metadata is stored in something called an inode, which is a unique number assigned to each file. The Linux kernel uses these inodes to keep track of and manage all the files on your system.

Hard Links vs. Soft Links ⛓️

Now, let’s talk about the two types of links you can create in Linux: hard links and soft links (also known as symbolic links).

Hard Links

When you create a hard link to a file, the new link will have the exact same inode number as the original file. This means they share the same metadata.

What’s really cool about hard links is that any changes you make to the content of one file will be reflected in the other. And if you delete the original file, the hard link will still work and you can access the content through it.

Soft Links (Symbolic Links)

Soft links are more like the shortcuts you’re used to in Windows. They have a different inode number from the original file, which means their metadata (like owner and permissions) will also be different.

With soft links, changes to the content are also bi-directional. However, if you delete the original file, the soft link will become invalid and you won’t be able to access the content through it anymore.

Linking vs. Copying

So, what’s the difference between linking and copying a file? Here are the key things to remember:

  • Changes: When you link files, changes are bi-directional. When you copy a file, changes to the copy don’t affect the original.
  • Location: A link always points to the original file’s location. A copy can be placed anywhere.
  • Permissions: The permissions on a linked file are the same as the original. The permissions on a copied file can be different.

Lab Demonstration

To show you how this all works in practice, I’ll walk you through a lab where I create both hard and soft links and show you how they behave.

Technical Commands Used

Here are the technical commands I used in the terminal during the demonstration:

  • touch file
  • echo "this is the original file"
  • cat original
  • ln original hard_link_original
  • ls -li
  • echo "added text to hard link" >> hl_original
  • cat hl_original
  • rm -f original
  • ln -s original soft_link_original
  • echo "changes pertinent to soft link" >> sl_original
  • cat sl_original

Video Walkthrough

 

About the Author

Mastermind Study Notes is a group of talented authors and writers who are experienced and well-versed across different fields. The group is led by, Motasem Hamdan, who is a Cybersecurity content creator and YouTuber.

View Articles