最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
软链接:
软的或象征性的更像是原始文件....的捷径如果您删除原始的快捷方式失败,如果您只删除快捷方式,原始的不会发生任何变化。
软链接语法:ln -s Pathof_Target_file链接
输出:link -> ./Target_file
证明:readlink链接 同样,在ls -l链接输出中,您将看到lrwxrwxrwx中的第一个字母为l,这表明该文件是一个软链接。
删除链路:unlink链路
注意:如果你愿意,你的软链接即使从当前目录移动到其他地方也可以工作。在创建软链接时,请确保您给出的是绝对路径而不是相对路径。即(从/root/user/Target_file开始,而不是。/Target_file)
硬链接:
硬链接更多的是镜像副本或同一文件的多条路径。对file1做一些操作,它就会出现在文件2中。 删除一个仍然可以保留另一个。
inode(或文件)只有在所有(硬)链接或(同一文件)inode的所有路径都已删除时才会被删除。
一旦创建了硬链接,该链接就具有原始文件的inode。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层inode。对inode上数据的任何更改都反映在引用该inode的所有文件中。
硬链接语法:ln Target_file链接
输出:将创建一个名称为link的文件,其索引节点号与Targetfile相同。
证明:ls -i link Target_file(检查它们的索引节点)
删除链接:rm -f link(像删除普通文件一样删除链接)
注意:符号链接可以跨文件系统,因为它们只是另一个文件的名称。而硬链接只在同一个文件系统中有效。
符号链接有一些硬链接所没有的特性:
Hard link point to the file content. while Soft link points to the file name. while size of hard link is the size of the content while soft link is having the file name size. Hard links share the same inode. Soft links do not. Hard links can't cross file systems. Soft links do. you know immediately where a symbolic link points to while with hard links, you need to explore the whole file system to find files sharing the same inode. # find / -inum 517333 /home/bobbin/sync.sh /root/synchro hard-links cannot point to directories.
硬链接有两个限制:
目录不支持硬链接。Linux不允许这样维护目录的非循环树结构。 不能跨文件系统创建硬链接。这两个文件必须在相同的文件系统上,因为不同的文件系统有不同的独立的inode表(两个文件在不同的文件系统上,但是具有相同的inode号将是不同的)。
其他回答
我推荐你们去维基百科:
符号链接 硬链接
以下几点:
与硬链接不同,符号链接可以跨文件系统(大多数情况下)。 符号链接可以指向目录。 硬链接指向一个文件,并允许您使用多个名称引用同一个文件。 只要至少有一个链接,数据就仍然可用。
当原始文件被移动时,硬链接非常有用。例如,将文件从/bin移动到/usr/bin或/usr/local/bin。到/bin中文件的任何符号链接都将被破坏,但是硬链接(直接到文件的inode的链接)不会关心。
硬链接可能占用更少的磁盘空间,因为它们只占用一个目录条目,而符号链接需要自己的inode来存储它所指向的名称。
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.
说到这里,恢复一个在进程a中打开,但在文件系统中未链接的文件需要使用硬链接来重新创建inode链接,这样当打开该文件的进程关闭或离开时,该文件不会消失。
符号链接为文件提供了另一个名称,在某种程度上类似于硬链接。但是,即使文件中仍然存在符号链接,也可以删除文件。
在文件系统下面,文件由inode表示。(或者是多个索引节点?不确定。)
文件系统中的文件基本上是一个到inode的链接。 因此,硬链接只是创建另一个文件,该文件具有指向相同底层inode的链接。
当您删除一个文件时,它会删除到底层inode的一个链接。只有当到inode的所有链接都被删除时,inode才会被删除(或可删除/可覆盖)。
符号链接是指向文件系统中另一个名称的链接。
一旦建立了硬链接,链接就指向inode。删除、重命名或移动原始文件不会影响硬链接,因为它链接到底层inode。对inode上数据的任何更改都反映在引用该inode的所有文件中。
注意:硬链接只在同一个文件系统内有效。符号链接可以跨文件系统,因为它们只是另一个文件的名称。
一个目录条目链接一个结构:
struct dentry{
ino_t ino;
char name[256];
}
ino是inode的编号,name是文件名,inode结构可能是这样的:
struct inode{
link_t nlink;
...
}
例如,你创建一个文件/1,目录条目可能是这样的:
struct dentry{
ino_t ino; /* such as 15 */
char name[256]; /* "1" */
}
inode结构可能是这样的:
struct inode{ /* inode number 15 */
link_t nlink; /* nlink = 1 */
...
}
然后你创建一个硬链接(可能是/100),目录条目可能是这样的:
struct dentry{
ino_t ino; /* 15 */
char name[256]; /* 100 */
}
inode结构可能是这样的:
struct inode{ /* inode numebr 15 */
link_t nlink; /* nlink = 2 */
...
}
然后你创建一个符号链接(可能是/200)到文件1,目录条目可能是这样的:
struct dentry{
ino_t ino; /* such as 16 */
char name[256]; /* "200" */
}
inode结构可能是这样的:
struct inode{ /* inode number 15 */
link_t nlink; /* nlink = 2 */
...
}
struct inode{ /* inode number 16 */
link_t nlink; /* nlink = 1 */
...
} /* the data of inode 16 maybe /1 or 1 */