Soft & Hard Links

A friendly guide to soft and hard links.

Piyush Kochhar
4 min readAug 3, 2020
Soft Links & Hard Links — Created by Piyush Kochhar

What are Links?

A link in an operating system is a pointer to a file. If you’ve ever worked with programming languages like C, ++ etc., you’ve probably programmed using pointers which are nothing but pointers to other files and directories. Operating systems like Windows, have concepts of shortcuts which at the end are nothing but a type of link.

The two kinds of links:

  • Soft Links or Symbolic Links
  • Hard Links

Before we look into links, let’s learn about inodes.

Inode

Inode is an entry in an inode table. It contains metadata about the file.

An inode structure looks something like:

Inode Structure

This structure contains information like file permissions, the owner or group of owners of the file, the locations size in bytes and inode creation, modification time etc.

Soft Links

Soft Links are like shortcuts in windows operating system. Whenever we create a soft link, the file/directory even looks the same: with a little curly arrow at the bottom.

Whenever we create a new soft link, the file points to the source file.

If any modifications are made to the source file, the changes are reflected back in the soft link file as well, which is the same behavior as shortcuts.

Soft Links can be created across different file systems, but if the source file is deleted or moved from its original location, the soft linked file will be rendered useless, become dangling, meaning it won’t display any data unless we put the source file in its original location.

Both the source file and the soft link have different inode numbers. That means they have different file permissions and owner info etc.

Soft Link contains the path for original file and not the contents.

Command for creating soft link:

$ ln -s [source filename] [link name]

Example:

Let’s create a new file:

$ touch file1.txt

Now let’s soft link them using following cmd

$ ln –s file1.txt file2.txt

Let’s see the current directory

$ ls –li

Here, we see both file1 and file2 have different inode numbers and file permissions and file2 points to file1.

Notice the inode number and file permissions in column 1 and 2 respectively!

inode number for file1: 8607576596; inode number for file2: 8607576802

Now, if we delete the source file i.e file1.txt

$ rm file1.txt

The link between file2.txt and file1.txt still stays, but if we try to view the contents of file2.txt

cat file2.txtcat: file2.txt: No such file or directory

Hence, file2.txt becomes useless.

Hard Links

Whenever we create a new hard link, the file points to the data on the hard disk. If any modifications are made to the source file, the changes are reflected back in the hard link file as well, but if any changes are made to the hard link file, the changes are reflected in the source file. Think of it something like “live syncing”.

Hard Links can’t be created across different file systems. If the source file is deleted or moved from its original location, the hard link still points to the data of the source file on the hard disk. And if we delete the hard link file, the data still persists.

Both the source file and the hard link have the same inode number. That means they have same file permissions and owner info, etc.

Hard Links point to the memory location of the contents of the source file.

We cannot create a hard link for a directory.

Command for creating hard link:

$ ln  [source filename] [link name]

Example:

Let’s create a new file:

$ touch file1.txt

Now let’s hard link them using the following cmd:

$ ln file1.txt file2.txt

Now let’s see the current directory

Here, both file1.txt and file2.txt have the same inode number, same permissions, and basically have the same properties.

Notice the inode number and file permissions in column 1 and 2 respectively!

inode number for file1: 8607576596; inode number for file2: 8607576596

But the main difference is that if we delete the source file here, i.e. file1.txt, file2.txt can still access to the contents that were inside of file1.txt and we won’t get any error upon accessing the contents of file2.txt as we did in soft links.

We can simply access contents using:

$ cat file2.txt

That’s all folks!

--

--