Hardlinks: Fun facts

Posted 3 years, 7 months ago | Originally written on 9 Mar 2021

First we will create a discardable directory calls rm.dir into which we shall add two directories: dir1 and dir2.

~$ mkdir rm.dir && cd rm.dir
~$ mkdir dir1 dir2

Now paste some rubbish into a dir1/file1.txt (find your least favourite website and copy and paste everything):

~$ cat > dir1/file1.txt
# paste some rubbish here
^D

Now create a hardlink to a file called dir2/file2.txt.

~$ ln dir1/file1.txt dir2/file2.txt

Let's view the tree view using tree.

~$ tree
.
├── another_file.txt
├── dir1
│ └── file1.txt
└── dir2
  └── file2.txt

2 directories, 3 files

Let's view the file stats using stat.

~$ stat dir*/*
16777220 99414274 -rw-r--r-- 2 pkorir pkorir 0 14322 "Mar 9 08:38:00 2021" "Mar 9 08:37:33 2021" "Mar 9 08:37:59 2021" "Mar 9 08:37:16 2021" 4096 32 0 dir1/file1.txt
16777220 99414274 -rw-r--r-- 2 pkorir pkorir 0 14322 "Mar 9 08:38:00 2021" "Mar 9 08:37:33 2021" "Mar 9 08:37:59 2021" "Mar 9 08:37:16 2021" 4096 32 0 dir2/file2.txt

The second column shows the files inode number. Notice that they are identical: these are hardlinks - two directory entries to the same data blocks i.e. one thing with two names. Let's make a third directory and do an rsync of dir2/file2.txt.

~$ mkdir dir3
~$ rsync -zarvh dir2/ dir3/
building file list ... done
./
file2.txt

sent 6.45K bytes received 48 bytes 13.00K bytes/sec
total size is 14.32K speedup is 2.20
~$ tree
.
├── another_file.txt
├── dir1
│ └── file1.txt
├── dir2
│ └── file2.txt
└── dir3
  └── file2.txt

3 directories, 4 files

What does stat tell us now?

~$ stat dir*/*
16777220 99414274 -rw-r--r-- 2 pkorir pkorir 0 14322 "Mar 9 08:38:00 2021" "Mar 9 08:37:33 2021" "Mar 9 08:37:59 2021" "Mar 9 08:37:16 2021" 4096 32 0 dir1/file1.txt
16777220 99414274 -rw-r--r-- 2 pkorir pkorir 0 14322 "Mar 9 08:38:00 2021" "Mar 9 08:37:33 2021" "Mar 9 08:37:59 2021" "Mar 9 08:37:16 2021" 4096 32 0 dir2/file2.txt
16777220 99414874 -rw-r--r-- 1 pkorir pkorir 0 14322 "Mar 9 08:39:03 2021" "Mar 9 08:37:33 2021" "Mar 9 08:39:01 2021" "Mar 9 08:37:33 2021" 4096 32 0 dir3/file2.txt

We have copied the hardlink to an actual file. How much disk space are we using?

~$ du -h
 16K  ./dir2
 16K  ./dir3
 0B  ./dir1
 36K  .

What?

~$ du -h dir*/*
 16K  dir1/file1.txt
 16K  dir3/file2.txt

Hardlinks don't take any disk space!? Cool!