官术网_书友最值得收藏!

  • 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!

主站蜘蛛池模板: 华蓥市| 应用必备| 务川| 大方县| 衡南县| 湘西| 永丰县| 惠州市| 怀来县| 社旗县| 金昌市| 山东省| 凌云县| 娱乐| 五大连池市| 耿马| 锡林浩特市| 博白县| 五莲县| 新建县| 江阴市| 富裕县| 富平县| 万源市| 浪卡子县| 进贤县| 建瓯市| 金门县| 滦南县| 大兴区| 昌图县| 花莲市| 合作市| 黄平县| 弥渡县| 平原县| 三台县| 正阳县| 丰镇市| 西和县| 都昌县|