- Learn Linux Quickly
- Ahmed AlKabary
- 874字
- 2021-06-11 18:43:53
Creating hard links
The story is a little bit different when it comes to hard links. That's because a hard link is a replica of the original file. And here is a definition of a hard link:
WHAT IS A HARD LINK?
A hard link is simply an additional name for an existing file. It has the same inode of the original file, and hence, it's indistinguishable from the original file.
You can think of it as a nickname. When somebody calls you by your nickname, they are still referring to you.
A hard link has the following properties:
- A hard link has (shares) the same inode of the original file.
- A hard link remains intact if the original file gets deleted.
- Any change in the hard link is reflected in the original file.
- You can't create hard links to directories.
The following diagram helps you visualize hard links:

Figure 4: A hard link visualization
We use the same ln command to create hard links, but this time we omit the -s option:
ln original_file hard_link
So to create a hard link named hard.txt to the file facts.txt, you can simply run the command ln facts.txt hard.txt:
elliot@ubuntu-linux:~$ ln facts.txt hard.txt
Now let's do a long listing on the hard link hard.txt and the original file facts.txt:
elliot@ubuntu-linux:~$ ls -l hard.txt
-rw-rw-r-- 2 tom tom 210 May 9 00:07 hard.txt
elliot@ubuntu-linux:~$ ls -l facts.txt
-rw-rw-r-- 2 tom tom 210 May 9 00:07 facts.txt
They are identical! The hard link also has the same contents just like the original file:
elliot@ubuntu-linux:~$ cat hard.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
Grass is green.
Now add the line "Swimming is a sport." to the very end of the hard link hard.txt with the text editor of your choice:
elliot@ubuntu-linux:~$ cat hard.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
Grass is green.
Swimming is a sport.
Now just like in the case with soft links, the content of the original file has also changed:
elliot@ubuntu-linux:~$ cat facts.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
Grass is green.
Swimming is a sport.
Now let's check the inode numbers of both files:
elliot@ubuntu-linux:~ ls -i hard.txt facts.txt
925155 facts.txt 925155 hard.txt
Notice that both files have the same inode number. Now let's run the stat command on both files:
elliot@ubuntu-linux:~$ stat hard.txt facts.txt
File: hard.txt
Size: 210 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 925155 Links: 2
Access: (0664/-rw-rw-r--) Uid: ( 1000/ elliot) Gid: ( 1000/ elliot)
Access: 2019-05-09 00:07:36.884000000 -0600
Modify: 2019-05-09 00:07:25.708000000 -0600
Change: 2019-05-09 00:07:25.720000000 -0600
Birth: -
File: facts.txt
Size: 210 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 925155 Links: 2
Access: (0664/-rw-rw-r--) Uid: ( 1000/ elliot) Gid: ( 1000/ elliot)
Access: 2019-05-09 00:07:36.884000000 -0600
Modify: 2019-05-09 00:07:25.708000000 -0600
Change: 2019-05-09 00:07:25.720000000 -0600
Birth: -
The output of the stat command is identical for both files. And also, the number of Links: 2 here means that there are two hard links to the file. Hmmm! We have only created one hard link to the file facts.txt, then how come it listed two hard links? Well, the original file is a hard link to itself, and so any file has a minimum of one hard link (itself).
Now unlike the case with soft links, if you delete the original file facts.txt:
elliot@ubuntu-linux:~$ rm facts.txt
The hard link remains intact:
elliot@ubuntu-linux:~$ cat hard.txt
Apples are red.
Grapes are green.
Bananas are yellow.
Cherries are red.
Sky is high.
Earth is round.
Linux is awesome!
Cherries are red.
Cherries are red.
Cherries are red.
Grass is green.
Swimming is a sport.
The following diagram shows you why the hard link remains intact.

Figure 5: hard.txt remains intact
Now notice that after the removal of the file facts.txt, the number of hard links count of the file hard.txt will decrease to one:
elliot@ubuntu-linux:~$ stat hard.txt
File: hard.txt
Size: 210 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 925155 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ elliot) Gid: ( 1000/ elliot)
Access: 2019-05-09 00:17:21.176000000 -0600
Modify: 2019-05-09 00:07:25.708000000 -0600
Change: 2019-05-09 00:17:18.696000000 -0600
Birth: -
You can't create a hard link to a directory. If you don't believe me, then try creating a hard link named variables to the /var directory:
elliot@ubuntu-linux:~$ ln /var variables
ln: /var: hard link not allowed for directory
I told you hard links are not allowed for directories! Why do you doubt me?
MIND-BLOWING FACT
There is NO WAY to differentiate between an original file and a hard link. For example, if you are given two files, and one of them happens to be a hard link for the other file, there is NO WAY to tell which file is the original! It is like the chicken and egg dilemma; no one knows which one came first!
- Learn ECMAScript(Second Edition)
- 測試驅動開發:入門、實戰與進階
- Python for Secret Agents:Volume II
- Android Studio Essentials
- Visual FoxPro 程序設計
- Django開發從入門到實踐
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- Learning Probabilistic Graphical Models in R
- Mastering Unity 2D Game Development(Second Edition)
- Mastering Backbone.js
- Python數據可視化之美:專業圖表繪制指南(全彩)
- Python編程基礎與數據分析
- Advanced Analytics with R and Tableau
- Python量化交易實戰:使用vn.py構建交易系統
- Mastering Python Scientific Computing