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

Creating soft links

Now since you understand what a file inode is, you can easily understand the concept of hard and soft links. And let us start with soft links:

WHAT IS A SOFT LINK?

A soft link (also referred to as a symbolic link) is simply a file that points to another file.

A picture is worth a thousand words, so the following diagram will help you visualize soft links.

Figure 1: A soft link visualization

To create a soft link, we use the ln command with the -s option as follows: 

ln -s original_file soft_link

So to create a soft link named soft.txt to the facts.txt file, you can run the command ln -s facts.txt soft.txt:

elliot@ubuntu-linux:~$ ln -s facts.txt soft.txt

Now let's do a long listing on the soft link file soft.txt that we just created:

elliot@ubuntu-linux:~$ ls -l soft.txt
lrwxrwxrwx 1 tom tom 9 May 8 21:48 soft.txt -> facts.txt

You will notice two things. First, the letter l in the first column of the output lrwxrwxrwx, which signals that the file is a link (soft link), and secondly you can see the right arrow soft.txt → facts.txt, which basically tells us that soft.txt is a soft link that points to the file facts.txt.

Now let's check the contents of the file soft.txt:

elliot@ubuntu-linux:~$ cat soft.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.

Of course, it contains the same data that the original file facts.txt has. In fact, if you edit the soft link, it will actually edit the original file as well.

To demonstrate, open the file soft.txt with any text editor and add the line "Grass is green." at the very end of the file, and then save and exit so the contents of soft.txt will be as follows:

elliot@ubuntu-linux:~$ cat soft.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 let's check the contents of the original file facts.txt:

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.

As you can see, the new line "Grass is green." is also there. That's because every time you edit a soft link, it actually edits the original file that it points to as well.

Now if you delete the soft link, nothing will happen to the original file, it remains intact:

elliot@ubuntu-linux:~$ rm soft.txt 
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.

Now let's create the soft link soft.txt again:

elliot@ubuntu-linux:~$ ln -s facts.txt soft.txt

If you delete the original file facts.txt, the soft link soft.txt will become useless! But before we delete the facts.txt file, let's make a copy of it in /tmp because we will need it later on:

elliot@ubuntu-linux:~$ cp facts.txt /tmp

Now let's delete the file facts.txt from elliot's home directory and see what happens to the soft link:

elliot@ubuntu-linux:~$ rm facts.txt 
elliot@ubuntu-linux:~$ cat soft.txt
cat: soft.txt: No such file or directory

As you can see, the soft link soft.txt becomes useless as it's now pointing to nowhere. Keep in mind that the file soft.txt still exists, as shown in the following screenshot.

Figure 2: soft.txt becomes useless!

The following diagram shows you that the soft link soft.txt points to nowhere after the original file facts.txt has been deleted.

Figure 3: soft.txt points to nowhere

Now if we moved facts.txt back to elliot's home directory:

elliot@ubuntu-linux:~$ mv /tmp/facts.txt /home/elliot

The soft link soft.txt will be useful again! You can say that we resurrected the soft link!

elliot@ubuntu-linux:~$ cat soft.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.

Let's compare the inode numbers of the soft link soft.txt and the original file facts.txt:

elliot@ubuntu-linux:~$ ls -i soft.txt facts.txt 
925155 facts.txt 924556 soft.txt

As you can see, the inode numbers of the two files are different. Finally, let's run the stat command on the soft link soft.txt:

elliot@ubuntu-linux:~$ stat soft.txt 
File: soft.txt -> facts.txt
Size: 9 Blocks: 0 IO Block: 4096 symbolic link
Device: 801h/2049d Inode: 924556 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 1000/ tom) Gid: ( 1000/ tom)
Access: 2019-05-08 22:04:58.636000000 -0600
Modify: 2019-05-08 22:02:18.356000000 -0600
Change: 2019-05-08 22:02:18.356000000 -0600
Birth: -

As you can see, it lists the file as a symbolic link, which is another name for a soft link.

So as you have seen so far, a soft link has the following properties:

  • The inode of a soft link is different from the original file.
  • A soft link becomes useless once the original file is deleted.
  • Any change to the soft link is actually a change in the original file.
  • You can create soft links to directories.

You can create soft links to directories the same way you can create soft links to files. To demonstrate, let's first create a directory named sports in elliot's home directory. And inside sports, create three files – swimming, soccer, and hockey – as follows:

elliot@ubuntu-linux:~$ mkdir sports
elliot@ubuntu-linux:~$ touch sports/swimming sports/soccer sports/hockey
elliot@ubuntu-linux:~$ ls sports

hockey soccer swimming

Now let's create a soft link named softdir1 to the sports directory:

elliot@ubuntu-linux:~$ ln -s sports softdir1

Now if you change to softdir1, you are actually changing to sports, and so you will see the same directory contents:

elliot@ubuntu-linux:~$ cd softdir1 
elliot@ubuntu-linux:~/softdir1$ ls
hockey soccer swimming

Of course, the same thing holds for directories as well; that is, if you delete the original directory, the soft link will become useless!

主站蜘蛛池模板: 怀来县| 资中县| 上犹县| 比如县| 黎川县| 高密市| 吉首市| 潞西市| 镇沅| 遵化市| 吉木萨尔县| 普兰店市| 绍兴县| 安福县| 农安县| 城口县| 开封市| 台州市| 河西区| 麟游县| 灵山县| 普格县| 弋阳县| 积石山| 南漳县| 长春市| 都江堰市| 东乌珠穆沁旗| 平泉县| 桂东县| 古蔺县| 宁化县| 思茅市| 宝清县| 六盘水市| 镶黄旗| 台州市| 清新县| 合水县| 澜沧| 左权县|