Inodes in Linux | Hard Links and Soft Links

Sachithra_Manamperi
5 min readNov 9, 2022

Create and Manage Hard Links

This Article is about How to create hard links and soft links in Linux along with a brief introduction to inodes.

So before going to hard links and soft links we have to get an idea about the inodes concept in Linux.

An inode is a data structure that keeps track of all the files and directories within a Linux or UNIX-based filesystem. So, every file and directory in a filesystem is allocated an inode, which is identified by an integer known as an “inode number”. These unique identifiers store metadata about each file and directory.

Points to note in inode :

  • Every file in the system has an inode(Index Node)
  • Contains all the file information except the file contents & name.
  • Just like a personal ID or a passport(Without a name!)

Inodes store metadata such as the following

  • Inode number
  • File size
  • Owner information
  • Permissions
  • File type
  • The number of links etc.

As explained above, each inode is identified by an inode number. Therefore, when creating or copying a file, Linux assigns a different inode number to the new file. However, when moving a file, the inode number will only change if the file is moved to a different filesystem. This applies to directories as well.

Let’s go ahead and see these using some commands. First, let’s create a file called test.jpg

Using the touch command we can create this file and add some text to it.

Using the stat command we can see there’s one link to the file we just created! We can see there’s an Inode number assigned to the file.

So in the stat command results, it shows the no of hard links that specific file has.

Basic Representation of an Inode

Hard Links

Quick definition: In the Linux operating system, a hard link is equivalent to a file stored in the hard drive — and it actually references or points to a spot on a hard drive. A hard link is a mirror copy of the original file. The distinguishing characteristic of a hard link from a soft link is that deleting the original file doesn’t affect a hard link, while it renders a soft link inoperable.

We saw that the test.jpg file has one hard link.

  • Different names of the same file
  • Same file size
  • Same iNode number

Note:- If the original File is deleted, the Hard link will still contain the data that were in the original file.

How to create a Hard Link?

I have two users on my machine. they are sachithra and fathima. How can sachithra share this picture with Fathima? The easy answer is to copy the file like this.

But can we do this for thousands of files? What a wastage of data huh?

so without copying the file we can hard link the file.

Before that, I should remove the copied file from /home/fathima/pictures

To create a hard link to /home/sachithra/pictures/test.jpg file we use the ln command

ln path_to_target_file path_to_link_file

Now data are stored only once but they can be accessed from different locations. Awesome!

Now let’s say sachithra deletes the test.jpg file

What will happen? after deletion still, the file has one hard link to Fathima. so Fathima still has access.

But when Fathima also delete the file,

Then the file has zero hard links. So the file will be deleted from the disk.

Limitations of hard links

  • You can only hard link to files, not directories/folders
  • You can only hard link to files on the same file system

Note: when you hard-link files make sure that all users have the required permissions to access the files

What are Soft Links?

Hard links point to an inode. but soft links point to the path of the file. soft links in Linux are very similar to shortcuts in windows.

Quick definition: In Linux, a soft link, also known as a symbolic link, is a special sort of file that points to a different file. In Windows vocabulary, you could think of it as a shortcut. Because the connection is a logical one and not a duplication, soft links can point at entire directories or links to files on remote computers. Hard links cannot do this.

Soft Links :

  • Just like a shortcut in Window
  • It is a pointer to the original file
  • Different iNode number
  • Smaller file size

Note: If we delete the original file, the soft link will be rendered useless!

ln -s path_to_target_file path_to_link_file

Features of soft links

  • soft link to files and folders
  • Soft link to files on different filesystems as well

Hope you guys get an idea about hard links and soft links.

Just go through my previous article as well👇

Don't forget to follow and share this with your enthusiastic friends 😁

--

--

Sachithra_Manamperi

Undergraduate | Software Engineering | Dharmaraja College Kandy | Sri Lankan