hardlink_name)。
stackoverlfow上,有对他们比较好的对比解释如下:
“Underneath the file system files are represented by inodes
(or is it multiple inodes not sure)A file in the file system is
basically a link to an inode.A hard link then just creates another
file with a link to the same underlying inode.
When you delete a file it removes one link to the underlying
inode. The inode is only deleted (or deletable/over-writable) when
all links to the inode have been deleted.
A symbolic link is a link to another name in the file
system.
Once a hard link has been made the link is to the inode.
deleting renaming or moving the original file will not affect the
hard link as it links to the underlying inode. Any changes to the
data on the inode is reflected in all files that refer to that
inode.
Note: Hard links are only valid within the same File System.
Symbolic links can span file systems as they are simply the name of
another file.”
“Hard links are useful when the original file is getting moved
around. For example, moving a file from /bin to /usr/bin or to
/usr/local/bin. Any symlink to the file in /bin would be broken by
this, but a hardlink, being a link directly to the inode for the
file, wouldn't care.
Hard links may take less disk space as they only take up a
directory entry, whereas a symlink needs its own inode to store the
name it points to.
Hard links also take less time to resolve - symlinks can point
to other symlinks that are in symlinked directories. And some of
these could be on NFS or other high-latency file systems, and so
could result in network traffic to resolve. Hard links, being
always on the same file system, are always resolved in a single
look-up, and never involve network latency (if it's a hardlink on
an NFS filesystem, the NFS server would do the resolution, and it
would be invisible to the client system). Sometimes this is
important. Not for me, but I can imagine high-performance systems
where this might be important.
I also think things like mmap(2) and even open(2) use the same
functionality as hardlinks to keep a file's inode active so that
even if the file gets unlink(2)ed, the inode remains to allow the
process continued access, and only once the process closes it does
the file really go away. This allows for much safer temporary files
(if you can get the open and unlink to happen atomically, which
there may be a POSIX API for that I'm not remembering, then you
really have a safe temporary file) where you can read/write your
data without anyone being able to access it. Well, that was true
before /proc gave everyone the ability to look at your file
descriptors, but that's another story.
Speaking of which, recovering a file that is open in process
A, but unlinked on the file system revolves around using hardlinks
to recreate the inode links so the file doesn't go away when the
process which has it open closes it or goes away.”
上面解释地比较清楚,我就稍微总结一下:
1、因为档案系统建立在node之上,文件夹和档案为两种不同形式。
2、hard link指向和源文件同一个node,symbolic
link则会新建一个node用来存放指向源文件node的相关信息
3、由上面可以看出,hard
link更加安全,因为所有link都是同级的,只有所有link都被删除,该文件才被真正删除,上面的答案中举了个移动文件的例子(可见,mv应该就是靠这个实现的)。而symbolic
link,因为指向实为嫁接之用,如果源文件被删除,链接也会失效(这东西类似windows中的快捷方式)
4、因为hard link这个特性,所以它只能链接档案,不能链接文件夹(因为hard
link指向源文件的node,假设可以链接文件夹,那么处理起来便会有两种情况,即档案情况以及文件夹情况,如此便会使情况变得复杂)。
5、symbolic link因为会创建node文件,所以占用更多空间,创建开销也比较大
======================================================================================