Deleting Hard Links on GNU/Linux, and a discussion about inodes

7 min

language: ja bn de en es hi ko pt ru zh-cn zh-tw

Hello, I'm incompetent.
When I tried to create a link using the ln command after a long time, I mistakenly omitted the -s option, which resulted in a hard link being created, so I will delete the unnecessary hard link.

Executing the ln command without any options creates a hard link.

alleycat:[haturatu]:~/git/hardlink$ ls
alleycat:[haturatu]:~/git/hardlink$ touch hardlink1
alleycat:[haturatu]:~/git/hardlink$ echo "You are an idiot" >> hardlink1 
alleycat:[haturatu]:~/git/hardlink$ cat hardlink1 
You are an idiot
alleycat:[haturatu]:~/git/hardlink$ ls -la
合計 12
drwxr-xr-x   2 haturatu haturatu 4096  1月 12 15:00 .
drwxr-xr-x 114 haturatu haturatu 4096  1月 12 15:00 ..
-rw-r--r--   1 haturatu haturatu   17  1月 12 15:01 hardlink1

I tried creating a file as shown above.

alleycat:[haturatu]:~/git/hardlink$ ln hardlink1 hardlink2
alleycat:[haturatu]:~/git/hardlink$ ls -la
合計 16
drwxr-xr-x   2 haturatu haturatu 4096  1月 12 15:02 .
drwxr-xr-x 114 haturatu haturatu 4096  1月 12 15:00 ..
-rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink1
-rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink2

This created a hard link.
Next, let's look at the standard output below.

-rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink1
-rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink2

The 2 in -rw-r--r-- 2 indicates the number of hard links. You can see that the value has increased compared to before the hard link was created.
And since this is just a link, it doesn't consume additional disk space. It stores a single piece of data, and the inode numbers are identical, pointing to the same file.
You can check the inode number with the -i option.

alleycat:[haturatu]:~/git/hardlink$ ls -lia
合計 16
13238408 drwxr-xr-x   2 haturatu haturatu 4096  1月 12 15:02 .
11406555 drwxr-xr-x 114 haturatu haturatu 4096  1月 12 15:00 ..
13265123 -rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink1
13265123 -rw-r--r--   2 haturatu haturatu   17  1月 12 15:01 hardlink2

What is an inode?

This is entirely dependent on the file system, so the above example is under a GNU/Linux environment. In BSD-based file systems, it might be called vnode, and in ZFS, it's znode, etc., which are slightly different, but historically speaking, it's probably fine to think of them as extensions of the original UNIX file system.

Unlike symbolic links, if you delete hardlink1, hardlink2 remains, and if you delete hardlink2, hardlink1 remains. In this regard, it seems the explanation is that the file system manages the file itself, but it's still a bit complex, isn't it?
However, while hard links are convenient in this way, symbolic links can refer to files on different disks, whereas hard links cannot be created across different disks.
To put it simply, based on the previous explanation, file systems operate on an inode basis within a single disk, so when it comes to different disks, they are outside the file system's jurisdiction and cannot be referenced due to the layered structure.
Therefore, if a file exists on a different disk, it becomes necessary to create a symbolic link.

From a file system perspective, this is commonly used during disk recovery operations to identify which inode number files were created or deleted by referencing past inode information.
Here's a trap: if a file is overwritten, its inode number will also be updated. This means that when creating backup scripts, for example, you might need to be mindful of creating scripts that do not involve overwrite processing. (I messed up once.)

Identifying Files with the Same Inode Number

You can check this with the find command.

alleycat:[haturatu]:~/git/hardlink$ find . -inum 13265123
./hardlink2
./hardlink1

Now, let's try deleting hardlink1.

alleycat:[haturatu]:~/git/hardlink$ rm -i hardlink1
rm: 通常ファイル 'hardlink1' を削除しますか? y
alleycat:[haturatu]:~/git/hardlink$ ls -lai
合計 12
13238408 drwxr-xr-x   2 haturatu haturatu 4096  1月 12 15:23 .
11406555 drwxr-xr-x 114 haturatu haturatu 4096  1月 12 15:00 ..
13265123 -rw-r--r--   1 haturatu haturatu   17  1月 12 15:01 hardlink2
alleycat:[haturatu]:~/git/hardlink$ cat hardlink2 
You are an idiot

Now, the hard link count is also 1, and it has been confirmed that hardlink2, which was originally created as a link to hardlink1 using ln hardlink1 hardlink2, still exists with the same information as hardlink1 and holds the same data.

Practical Applications

I revisited this after a long time, and it seems that if you want to copy large files within the same disk but reduce disk consumption, hard links are a good option.
Well, it might not be something you consciously use often, but it's interesting because it helps you understand a bit about the file system's structure.


Until next time.
Best regards.

Related Posts