Hard link and Symbolic link

Files are arranged in directories (or folders if you prefer that term), and each file can be reached through a series of directories and sub-directories from the root - correct? Yes ... BUT ... there are some times that the same file can be reached through several names, and on Unix and Linux systems this is known as a "link ".

There are two ways a link can be set up.

A Hard Link is where a file has two names which are both on an equal weighting, and both of the file names in the "inode table" point directly to the blocks on the disc that contain the data.

You set up a hard link with an ln command without options - if the file ab.txt already exists and you want to give an additional name (hard link) to it, you'll write:

ln ab.txt cd.txt

and then both names will have equal ranking. The only way you'll know that there's a link there is by doing a long listing and you'll see a link count of 2 rather than 1, and if you need to find out what's linked to what, use the -i option to ls.

A Symbolic Link is where a file has one main name, but there's an extra entry in the file name table that refers any accesses back to the main name. This is slighly slower at runtime that a hard link, but it's more flexible and much more often used in day to day admin work.

Essentially, the symbolic link is a pointer to the other file. If you are familiar with the Windows operating system, you may be reminded of shortcuts, which are very similar to symbolic links.

Symbolic links are set up using the ln command with the -s option - so for example:

ln -s ab.txt cd.txt

will set up a new name cd.txt that points to the (existing) file ab.txt. If you do a log listing (ls -l) of a directory that contains a symbolic link, you'll be told that it's a symbolic link with an "l" in the first column, and you'll be told where the file links to in the file name column. Very easy to spot!

Linux Administrators need to be aware of both symbolic and hard links; some of the operating system's files have two names and are installed as hard links, and if the administrator's installing software and wishes to do so under a generic name, there are good arguments for him to do so via a symbolic link.

Symbolic links are also a relatively painless way of placing files onto a disc partition (slice) that's not the one you would expect from the name through which you reference them - which is great if one of your partitions has got nearly full but there's space elsewhere, or if you want to position something on a partition that's got a different backup strategy / system in place to the one of the default location.

 

Using Links: An Example


Suppose that Rebecca and Nathan are working on a project together, and they need to share the file project.index, which is in Nathan’s home directory Rebecca could make a copy of the file for herself, but then if she makes any changes to the file, Nathan won’t see them in his copy So instead, Rebecca makes a link (a hard link) to that file. Now she has a file called project.index in her home directory, too, even though there is only one copy of the information in that file saved on the disk. This means that if Rebecca makes any changes in the file, Nathan will see those changes, too. However, the file can be found in two different places in the file system—in Nathan’s home directory, and in Rebecca’s. If Nathan deletes the file from his home directory, Rebecca will still have the file in her directory If she deletes her file, too, then it will really be gone.


Now, suppose there is another file in Nathan’s directory they need to share, called project.data. This time, Rebecca makes a symbolic link to the file, and calls it project.data.symlink. Rebecca can still make changes to the file, and Nathan will be able to see them. However, if Nathan deletes the project.data, Rebecca won’t have the file anymore, either. If she tries to use project.data.symlink after the original file is deleted, she will get an error message.


For an existing file named myname, which is determined by the inode (index node) N1, both links are created. The hard link hardlink is another name for the file myfile, and it corresponds to the same inode N1. The symbolic link symlink represents another file determined by the inode N2; its contents point to the file myfile.

What will happen if the file myfile is deleted? Actually, only the filename "myfile" will be deleted; the file itself remains with its other name hardlink (the file content remains unchanged). The symbolic link symlink is now broken; it points nowhere (there is no more referenced file myfile).

What will happen if another file named myfile is created in the same directory? This is a brand new file, determined by the new index node N3 and unrelated to the existing file hardlink, which continues to exist as a different file. However, the file symlink is now linked with the new file myname, and it continues to point to the newly created file myfile.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值