Recently I was asked this during a job interview. I was honest and said I knew how a symbolic link behaves and how to create one, but do not understand the use of a hard link and how it differs from a symbolic one.
最近我在一次求职面试中被问到这个问题。我很诚实,并且说我知道一个符号链接的行为和如何创建一个,但是不理解硬链接的使用和它与一个符号链接的区别。
21 个解决方案
#1
632
Underneath the file system files are represented by inodes (or is it multiple inodes not sure)
文件系统文件的下面是inode(或者是多个inode不确定)
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.
文件系统中的文件基本上是一个到inode的链接。然后,一个硬链接创建另一个文件,并链接到相同的底层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.
当您删除一个文件时,它将删除一个链接到底层的inode。当所有指向inode的链接被删除时,inode只被删除(或可删除/重写)。
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.
一旦一个硬链接被链接到inode。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层的inode。对inode上的数据的任何更改都反映在所有引用该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.
注意:硬链接只在同一个文件系统中有效。符号链接可以跨文件系统,因为它们只是另一个文件的名称。
#2
359
Some nice intuition that might help, using any Linux(ish) console.
使用任何Linux(ish)控制台的一些不错的直觉可能会有所帮助。
Create two files:
创建两个文件:
$ touch blah1; touch blah2
Enter some Data into them:
向他们输入一些数据:
$ echo "Cat" > blah1
$ echo "Dog" > blah2
(Actually, I could have used echo in the first place, as it creates the files if they don't exist... but never mind that.)
(实际上,我可以首先使用echo,因为如果它们不存在,就会创建文件……)但没关系。)
And as expected:
像预期的那样:
$cat blah1; cat blah2
Cat
Dog
Let's create hard and soft links:
让我们创建硬的和软的链接:
$ ln blah1 blah1-hard
$ ln -s blah2 blah2-soft
Let's see what just happened:
让我们看看发生了什么:
$ ls -l
blah1
blah1-hard
blah2
blah2-soft -> blah2
Changing the name of blah1 does not matter:
改变blah1的名称并不重要:
$ mv blah1 blah1-new
$ cat blah1-hard
Cat
blah1-hard points to the inode, the contents, of the file - that wasn't changed.
blah1-hard指向inode,文件的内容,没有改变。
$ mv blah2 blah2-new
$ ls blah2-soft
blah2-soft
$ cat blah2-soft
cat: blah2-soft: No such file or directory
The contents of the file could not be found because the soft link points to the name, that was changed, and not to the contents. Likewise, If blah1 is deleted, blah1-hard still holds the contents; if blah2 is deleted, blah2-soft is just a link to a non-existing file.
无法找到文件的内容,因为软链接指向名称,更改了,而不是内容。同样,如果blah1被删除,blah1-hard仍然保留内容;如果删除了blah2, blah2-soft只是一个与非现有文件的链接。
#3
308
As the saying goes, a picture is worth a thousand words. Here is how I visualize it:
俗话说,一幅画胜过千言万语。下面是我的设想:
Here is how we get to that picture:
下面是我们如何看待这幅图景:
-
Create a name
myfile.txt
in the file system that points to a new inode (which contains the metadata for the file and points to the blocks of data that contain its contents, i.e. the text "Hello, World!":myfile创建一个名称。文件系统中的txt指向一个新的inode(包含文件的元数据并指向包含其内容的数据块,即文本“Hello, World!”)
$ echo 'Hello, World!' > myfile.txt
-
Create a hard link
my-hard-link
to the filemyfile.txt
, which means "create a file that should point to the same inode thatmyfile.txt
points to":创建一个硬链接,硬链接到文件myfile。txt,意思是“创建一个文件,该文件应该指向myfile相同的inode”。txt指向”:
$ ln myfile.txt my-hard-link
-
Create a soft link
my-soft-link
to the filemyfile.txt
, which means "create a file that should point to the filemyfile.txt
":创建一个软链接软链接到文件myfile。txt,意思是“创建一个文件,该文件应该指向文件myfile.txt”:
$ ln -s myfile.txt my-soft-link
Look what will now happen if myfile.txt
is deleted (or moved): my-hard-link
still points to the same contents, and is thus unaffected, whereas my-soft-link
now points to nothing. Other answers discuss the pros/cons of each.
看,如果我的文件。txt被删除(或移动):my-硬链接仍然指向相同的内容,因此不受影响,而my-软链接现在指向什么都没有。其他答案讨论每一个的优点/缺点。
#4
62
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.
当原始文件被移动时,硬链接是有用的。例如,将文件从/bin转移到/usr/bin或到/ usr/local/bin。任何与/bin中的文件的符号链接都将被打破,但是一个硬链接,直接链接到该文件的inode,就不关心了。
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.
硬链接可能占用较少的磁盘空间,因为它们只占用一个目录条目,而symlink需要它自己的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.
硬链接也需要更少的时间来解析——符号链接可以指向在符号链接目录中的其他符号链接。其中一些可能是在NFS或其他高延迟的文件系统上,因此可能导致网络流量的解决。硬链接总是在同一个文件系统上,总是在单个查找中得到解决,而且永远不会涉及网络延迟(如果是NFS文件系统上的硬链接,那么NFS服务器将执行该解析,而客户机系统将看不到它)。有时这是很重要的。对我来说不是这样,但我可以想象出高性能系统的重要性。
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.
我也认为诸如mmap(2)甚至开放(2)使用相同的功能作为硬连接保持文件的inode活跃,这样即使文件被分离(2)ed,inode仍然允许进程继续访问,并只有一次关闭它的文件真的消失。这允许更安全临时文件(如果你能得到的开放和断开自动发生,它可能有一个POSIX API,我不记得,那么你真的有一个安全的临时文件),你可以读/写数据没有人能够访问它。这是真的,之前/proc让每个人都能查看你的文件描述符,但那是另一个故事。
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.
说到这个,恢复一个在进程a中打开的文件,但是在文件系统上没有链接,它围绕着使用硬链接来重新创建inode链接,这样文件在打开或关闭的过程中不会消失。
#5
31
A simple way to see the difference between a hard link and a symbolic link is through a simple example. A hard link to a file will point to the place where the file is stored, or the inode of that file. A symbolic link will point to the actual file itself.
一个简单的方法可以看出硬链接和符号链接之间的区别。文件的硬链接指向存储文件的位置,或该文件的inode。一个符号链接将指向实际的文件本身。
So if we have a file called "a" and create a hard link "b" and a symbolic link "c" which all refer to file "a" :
因此,如果我们有一个名为“a”的文件,并创建一个硬链接“b”和一个符号链接“c”,它们都指向文件“a”:
echo "111" > a
ln a b
ln -s a c
The output of "a", "b", and "c" will be :
“a”、“b”、“c”的输出为:
cat a --> 111
cat b --> 111
cat c --> 111
Now let's remove file "a" and see what happens to the output of "a", "b", and "c":
现在让我们删除文件a,看看“a”、“b”和“c”的输出会发生什么变化:
rm a
cat a --> No such file or directory
cat b --> 111
cat c --> No such file or directory
So what happened?
那么发生了什么?
Because file "c" points to file "a" itself, if file "a" is deleted then file "c" will have nothing to point to, in fact it is also deleted.
因为文件“c”指向文件“a”本身,如果文件“a”被删除,那么文件“c”将没有任何指向,实际上它也被删除了。
However, file "b" points to the place of storage, or the inode, of file "a". So if file "a" is deleted then it will no longer point to the inode, but because file "b" does, the inode will continue to store whatever contents belonged to "a" until no more hard links point to it anymore.
然而,文件“b”指向存储的位置,或文件“a”的inode。因此,如果文件“a”被删除,那么它将不再指向inode,而是因为file“b”做了,inode将继续存储属于“a”的任何内容,直到不再有任何硬链接指向它为止。
#6
28
Soft Link:
软链接:
soft or symbolic is more of a short cut to the original file....if you delete the original the shortcut fails and if you only delete the short cut nothing happens to the original.
软或符号更捷径的原始文件....如果你删除了原始的快捷方式失败,如果你只删除了短切,没有任何事情发生在原始。
Soft link Syntax: ln -s Pathof_Target_file link
软链接语法:ln -s Pathof_Target_file链接。
Output : link -> ./Target_file
输出:link -> ./Target_file。
Proof: readlink link
Also in ls -l link
output you will see the first letter in lrwxrwxrwx
as l which is indication that the file is a soft link.
证明:readlink链接也在ls -l链接输出中,您将在lrwxrwxrwx中看到第一个字母l,表示该文件是一个软链接。
Deleting the link: unlink link
删除链接:取消链接。
Note: If you wish, your softlink can work even after moving it somewhere else from the current dir. Make sure you give absolute path and not relative path while creating a soft link. i.e.(starting from /root/user/Target_file and not ./Target_file)
注意:如果您愿意,您的软链接可以在将它移动到当前目录下的其他地方之后继续工作。确保在创建软链接时提供绝对路径,而不是相对路径。即。(从/ root/user/target_file开始,而不是。/Target_file)
Hard Link:
硬链接:
Hard link is more of a mirror copy or multiple paths to the same file. Do something to file1 and it appears in file 2. Deleting one still keeps the other ok.
硬链接更多的是一个镜像拷贝或多个路径到同一个文件。对file1做一些事情,它会出现在文件2中。删除一个仍然保留另一个ok。
The inode(or file) is only deleted when all the (hard)links or all the paths to the (same file)inode has been deleted.
当所有(硬)链接或所有(相同文件)inode的路径被删除时,inode(或文件)才会被删除。
Once a hard link has been made the link has the inode of the original file. 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.
一旦一个硬链接被建立,链接就有了原始文件的索引。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层的inode。对inode上的数据的任何更改都反映在所有引用该inode的文件中。
Hard Link syntax: ln Target_file link
硬链接语法:ln Target_file链接。
Output: A file with name link will be created with the same inode number as of Targetfile.
输出:将使用与Targetfile相同的inode号创建一个带有名称链接的文件。
Proof: ls -i link Target_file
(check their inodes)
证明:ls -i链接Target_file(检查它们的inode)
Deleting the link: rm -f link
(Delete the link just like a normal file)
删除链接:rm -f链接(删除链接,就像普通文件一样)
Note: Symbolic links can span file systems as they are simply the name of another file. Whereas hard links are only valid within the same File System.
注意:符号链接可以跨文件系统,因为它们只是另一个文件的名称。而硬链接只在同一个文件系统中有效。
Symbolic links have some features hard links are missing:
符号链接有一些硬链接缺失的特征:
- 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.
- 硬链接共享相同的inode。软链接。
- 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.
- 您知道,当一个符号链接指向硬链接时,您需要探索整个文件系统来查找共享相同inode的文件。
- hard-links cannot point to directories.
- 硬链接不能指向目录。
Reason why hard-links can't cross file systems or partitions:
为什么硬链接不能跨文件系统或分区:
On a hard disk there are lots of sectors.
在硬盘上有很多扇区。
Say a file starts at inode (sector) 4001 and ends at 5000. The file is "/export/home/john/mail.doc"
说一个文件从inode(扇区)4001开始,到5000结束。该文件“/出口/ home /约翰/ mail.doc”
Then: 1. A hard link to "mail.doc" which is named "hardLinkToMail" contains the value: "4001". 2. A soft link to "mail.doc" which is named "softLinkToMail" contains the value: "/export/home/john/mail.doc".
:1。硬链接到“邮件”。名为“hardLinkToMail”的文档包含了“4001”的值。2。软链接到“邮件”。“doc”是“softLinkToMail”,包含“/export/home/john/mail.doc”的价值。
In 1) the hard link can only point to the same disk. It can not point to another drive. All drives has an inode of value "4001", how can the hard link distinguish between all discs? Which drive's "4001" is it?
在1)硬链接只能指向同一个磁盘。它不能指向另一个驱动器。所有驱动器都有一个值“4001”,硬链接如何区分所有磁盘?哪一个驱动器的“4001”是它?
In 2) the soft link contains a string. The string can point to another filesystem on another drive, because the full path is specified.
在2)软链接包含一个字符串。该字符串可以指向另一个驱动器上的另一个文件系统,因为指定了完整的路径。
#7
26
Symbolic links link to a path name. This can be anywhere in a system's file tree, and doesn't even have to exist when the link is created. The target path can be relative or absolute.
符号链接指向路径名。这可以是系统文件树中的任何地方,而且在创建链接时甚至不需要存在。目标路径可以是相对的,也可以是绝对的。
Hard links are additional pointers to an inode, meaning they can exist only on the same volume as the target. Additional hard links to a file are indistinguishable from the "original" name used to reference a file.
硬链接是指向inode的额外指针,这意味着它们只能以与目标相同的音量存在。对文件的附加硬链接与引用文件的“原始”名称难以区分。
#8
18
I would point you to Wikipedia:
我想指出*:
- Symbolic link
- 符号链接
- Hard link
- 硬链接
A few points:
几个点:
- Symlinks, unlike hard links, can cross filesystems (most of the time).
- 与硬链接不同,符号链接可以跨文件系统(大部分时间)。
- Symlinks can point to directories.
- 符号链接可以指向目录。
- Hard links point to a file and enable you to refer to the same file with more than one name.
- 硬链接指向一个文件,使您可以使用多个名称引用相同的文件。
- As long as there is at least one link, the data is still available.
- 只要有至少一个链接,数据仍然可用。
#9
8
Hard links are very useful when doing incremental backups. See rsnapshot, for example. The idea is to do copy using hard links:
在进行增量备份时,硬链接非常有用。例如,看到rsnapshot。这个想法是用硬链接复制:
- copy backup number n to n + 1
- 复制备份号码n到n + 1。
- copy backup n - 1 to n
- 复制备份n - 1到n。
- ...
- …
- copy backup 0 to backup 1
- 复制备份0到备份1。
- update backup 0 with any changed files.
- 更新备份0与任何更改的文件。
The new backup will not take up any extra space apart from any changes you've made, since all the incremental backups will point to the same set of inodes for files which haven't changed.
新的备份不会占用您所做的任何更改之外的任何额外空间,因为所有增量备份都将指向相同的文件集,而这些文件没有改变。
#10
5
Hard links can be useful, because it enables you to access the file from multiple different locations. it access inode directly. Some restrictions are :
硬链接可能是有用的,因为它使您能够从多个不同的位置访问文件。它直接访问inode。一些限制是:
- Hard links must exist all on the same device.
- 硬链接必须存在于同一设备上。
- We cannot create hard links to directories.
- 我们不能为目录创建硬链接。
- The number of aliases the original file has. When the last name is removed, the contents are also removed.
- 原始文件的别名数目。当删除最后一个名称时,内容也会被删除。
The nice thing about hard links is that no difference exists between the first hard link and the second hard link. They are both just hard links, and if the first hard link that ever existed for a file is removed, that does not impact the other hard links that still exist. The Linux operating system uses links on many locations to make files more accessible.
硬链接的优点在于,第一个硬链接和第二个硬链接之间没有区别。它们都是硬链接,如果文件的第一个硬链接被删除,它不会影响到其他仍然存在的硬链接。Linux操作系统在许多地方使用链接,使文件更容易访问。
Where as a symbolic link (also referred to as soft link) does not link directly to the inode but to the name of the file.The major disadvantage is that when the original file is removed, the symbolic link becomes invalid and does not work any longer.
作为一个符号链接(也称为软链接),它不直接链接到inode,而是链接到文件的名称。主要缺点是当删除原始文件时,符号链接无效,不再工作。
Some information on Inode:
一些索引节点信息:
Linux stores administrative data about files in inodes. Every file on Linux has an inode, and in the inode, important information about the file is stored:
Linux存储关于inode文件的管理数据。Linux上的每个文件都有一个inode,在inode中存储了关于文件的重要信息:
- The data block where the file contents are stored
- 存储文件内容的数据块。
- The creation, access, and modification date
- 创建、访问和修改日期。
- Permissions
- 权限
- File owners
- 文件所有者
Just one important piece of information is not stored in the inode: the name. Names are stored in the directory, and each filename knows which inode it has to address to access further file information. It is interesting to know that an inode does not know which name it has; it just knows how many names are associated with the inode. These names are referred to as hard links. When you create a file, you give it a name. Basically, this name is a hard link.
只有一个重要的信息没有存储在inode中:名称。名称存储在目录中,每个文件名知道它必须处理哪些inode以访问进一步的文件信息。很有趣的是,一个inode不知道它的名字;它只知道有多少个名称与inode相关联。这些名称被称为硬链接。当你创建一个文件,你给它一个名字。基本上,这个名字是一个硬链接。
#11
4
I add on Nick's question: when are hard links useful or necessary? The only application that comes to my mind, in which symbolic links wouldn't do the job, is providing a copy of a system file in a chrooted environment.
我补充了尼克的问题:什么时候硬链接有用或必要?在我的脑海中,只有符号链接不能完成工作的应用程序,在一个有根的环境中提供了一个系统文件的副本。
#12
3
Also:
另外:
- Read performance of hard links is better than symbolic links (micro-performance)
- 读取硬链接的性能优于符号链接(微性能)
- Symbolic links can be copied, version-controlled, ..etc. In another words, they are an actual file. On the other end, a hard link is something at a slightly lower level and you will find that compared to symbolic links, there are less tools that provide means for working with the hard links as hard links and not as normal files
- 符号链接可以复制,版本控制,等等。换句话说,它们是一个实际的文件。在另一端,硬链接的级别稍低一些,您会发现与符号链接相比,有更少的工具提供了与硬链接一起工作的方法,这是硬链接,而不是普通文件。
#13
3
What you think of as an ordinary "file" is actually two separate things: The data of a file, and a directory entry. When you create a hard link for a file, you actually create a second directory entry which refers to the same data. Both directory entries have the exact same functionality; each one can be used to open the file to read it. So you don't really have "a file plus a hard link", you have "file data with two directory entries". What you think of as deleting a file actually deletes a directory entry, and when the last directory entry for the data is deleted, then the data itself is deleted as well. For ordinary files that have only one directory entry, deleting the directory entry will delete the data as always. (While a file is opened, the OS creates a temporary link to the file, so even when you delete all directory entries, the data stays but disappears as soon as you close the file).
您所认为的普通“文件”实际上是两个独立的东西:一个文件的数据和一个目录条目。当您为一个文件创建一个硬链接时,您实际上创建了第二个目录条目,该条目引用相同的数据。两个目录项都具有完全相同的功能;每个都可以用来打开文件来读取它。所以你没有“一个文件加上一个硬链接”,你有“两个目录条目的文件数据”。您认为删除一个文件实际上删除了一个目录条目,当数据的最后一个目录条目被删除时,数据本身也会被删除。对于只有一个目录条目的普通文件,删除目录项将始终删除数据。(当一个文件被打开时,操作系统会创建一个临时的文件链接,所以即使你删除了所有的目录条目,数据也会保留,但是一旦你关闭了文件,它就会消失)。
As an example, create a file A.txt, a hard link B.txt, and delete A.txt. When you created A.txt, some data was created, and a directory entry A.txt. When you created the hard link, another directory entry B.txt was created, pointing to the exact same data. When you delete A.txt, you still have all the data and a single directory entry B.txt, exactly as if you had create a file B.txt in the first place.
例如,创建一个文件a。txt,一个硬链接B。txt和删除A.txt。当你创建了一个。txt,一些数据被创建,一个目录条目A.txt。当您创建硬链接时,另一个目录条目B。txt被创建,指向完全相同的数据。当你删除一个。txt,你仍然有所有的数据和一个目录条目B。txt,就好像你创建了一个文件B。首先是txt。
A soft link is just an (almost) ordinary file, except that it doesn't contain data, but the path of another directory entry. If you delete the file that the soft link refers to, then the soft link will contain a path that doesn't point to a directory entry anymore; it is broken. If you delete the soft link, it's like deleting any other file, the file it points to is unaffected.
软链接只是一个(几乎)普通的文件,只是它不包含数据,而是另一个目录条目的路径。如果删除软链接引用的文件,那么软链接将包含一条路径,不再指向目录条目;它坏了。如果删除软链接,就像删除其他文件一样,它指向的文件不会受到影响。
#14
2
Simply , Hard link : is just add new name to a file, that's mean , a file can have many name in the same time, all name are equal to each other, no one preferred, Hard link is not mean to copy the all contents of file and make new file is not that, it just create an alternative name to be known..
简单,硬链接:只是新名称添加到一个文件,这是说,一个文件可以在同一时间有很多的名字,名字都是相等的,没有人喜欢,硬链接并不意味着复制文件的所有内容,使新文件不是,它只是创建另一个名字叫. .
Symbolic link (symlink) : is a file pointer to another file, if the symbolic link points to an existing file which is later deleted, the symbolic link continues to point to the same file name even though the name no longer names any file.
符号链接(symlink):是指向另一个文件的文件指针,如果符号链接指向一个已存在的文件,该文件随后被删除,符号链接仍然指向相同的文件名,即使该名称不再命名任何文件。
#15
2
From MSDN,
Symbolic link
A symbolic link is a file-system object that points to another file system object. The object being pointed to is called the target.
符号链接是指向另一个文件系统对象的文件系统对象。被指向的对象称为目标。
Symbolic links are transparent to users; the links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner.
符号链接对用户来说是透明的;链接显示为正常的文件或目录,并且可以由用户或应用程序以完全相同的方式进行操作。
Symbolic links are designed to aid in migration and application compatibility with UNIX operating systems. Microsoft has implemented its symbolic links to function just like UNIX links.
符号链接旨在帮助迁移和应用程序兼容UNIX操作系统。微软已经实现了它的符号链接,就像UNIX链接一样。
Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name; relative links are determined relative to where relative–link specifiers are in a specified path
符号链接可以是绝对链接,也可以是相对链接。绝对链接是指定路径名的每一部分的链接;相对链接是确定的,相对链接说明符在指定的路径中。
An example of Absolute Symbolic Link
一个绝对符号链接的例子。
X: "C:\alpha\beta\absLink\gamma\file"
Link: "absLink" maps to "\\machineB\share"
Modified Path: "\\machineB\share\gamma\file"
An example of Relative Symbolic Links
一个相对符号链接的例子。
X: C:\alpha\beta\link\gamma\file
Link: "link" maps to "..\..\theta"
Modified Path: "C:\alpha\beta\..\..\theta\gamma\file"
Final Path: "C:\theta\gamma\file"
Hard link
A hard link is the file system representation of a file by which more than one path references a single file in the same volume.
硬链接是一个文件的文件系统表示,该文件的多个路径引用同一卷中的单个文件。
To create a hard link in windows, navigate to where link is to be created and enter this command:
要在windows中创建一个硬链接,导航到要创建链接的地方,并输入以下命令:
mklink /H Link_name target_path
Note that you can delete hard links any order, regardless of the order in which they were created. Also, hard links can not be created when
请注意,您可以删除硬链接任何顺序,不管它们创建的顺序是什么。同时,也不能创建硬链接。
- references are in different local drives
- 引用位于不同的本地驱动器中。
- references include network drive. In other words, one of the references is a network drive
- 引用包括网络驱动器。换句话说,其中一个引用是网络驱动器。
- hard link to be created is in the same path as the target
- 要创建的硬链接与目标的路径相同。
Junction
NTFS supports another link type called junction. MSDN defines it as follows:
NTFS支持另一种称为连接的链接类型。MSDN将其定义如下:
A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links.
一个连接(也称为软链接)不同于硬链接,因为它引用的存储对象是单独的目录,一个连接可以链接位于同一计算机上不同本地卷上的目录。否则,连接操作与硬链接相同。
The bolded parts in hard link section and junction section show the basic difference between the two.
硬连接部分和连接部分的粗体部分显示了两者之间的基本区别。
Command to create a junction in windows, navigate to where link is to be created and then enter:
命令在windows中创建一个连接,导航到要创建链接的地方,然后输入:
mklink /J link_name target_path
#16
2
A directory entry is link a structrue:
一个目录条目链接一个结构:
struct dentry{
ino_t ino;
char name[256];
}
the ino is the number of inode, the name is the file name, inode structure maybe like:
伊诺是索引节点的数量,名字是文件名,inode结构可能就像:
struct inode{
link_t nlink;
...
}
for example you creat a file /1, the directory entry maybe like:
例如,您创建了一个文件/1,目录条目可能如下:
struct dentry{
ino_t ino; /* such as 15 */
char name[256]; /* "1" */
}
the inode struct maybe like:
inode结构可能是这样的:
struct inode{ /* inode number 15 */
link_t nlink; /* nlink = 1 */
...
}
then you create a hard link(may be /100), the directory entry maybe like:
然后创建一个硬链接(可能是/100),目录条目可能是:
struct dentry{
ino_t ino; /* 15 */
char name[256]; /* 100 */
}
the inode struct maybe like:
inode结构可能是这样的:
struct inode{ /* inode numebr 15 */
link_t nlink; /* nlink = 2 */
...
}
then you create a symbolic link(may be /200) to file 1, the directory entry maybe like:
然后创建一个符号链接(可能是/200)到文件1,目录条目可能是:
struct dentry{
ino_t ino; /* such as 16 */
char name[256]; /* "200" */
}
the inode struct maybe like:
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 */
#17
2
Adding to all the above answers, the difference in finding the hardlink and softlink file can be understood as below:
在以上所有的答案中,找到硬链接和软链接文件的区别可以理解如下:
I have a file f6
in my current directory, as well as a directory named t2
.
我在当前目录中有一个文件f6,以及一个名为t2的目录。
File named f1
and ./t2/f2
are symbolic links to f6
.
文件命名为f1和。/t2/f2是f6的符号链接。
File named f7
and ./t2/f8
are hard links of f6
.
文件名f7和/t2/f8是f6的硬链接。
To find soft as well as hard link we can use:
要找到软的和硬的链接,我们可以使用:
$ find -L . -samefile f6
> ./f1
> ./f6
> ./f7
> ./t2/f2
> ./t2/f8
To find only hardlink we can use:
找到我们可以使用的硬链接:
$ find . -xdev -samefile f6
> ./f6
> ./f7
> ./t2/f8
Since hardlinks can be created on the same file system, we can search all the hardlinks without -L
option used (with -xdev
option) in the same file-system/mount-point. It saves the unnecessary search into different mount points.
由于可以在同一个文件系统上创建硬链接,所以我们可以在相同的文件系统/安装点中搜索所有的硬链接,而不使用-L选项(和-xdev选项)。它将不必要的搜索保存到不同的挂载点。
So searching the hardlink is somewhat faster then searching the softlinks(Please rectify if I am wrong or not clear).
所以搜索硬链接要比搜索软链接快一些(如果我错了或者不清楚,请纠正)。
#19
1
Symbolic links give another name to a file, in a way similar to hard links. But a file can be deleted even if there are remaining symbolic links.
符号链接以类似于硬链接的方式给文件提供另一个名称。但即使存在符号链接,也可以删除文件。
#20
0
I just found an easy way to understand hard links in a common scenario, software install.
我只是找到了一种简单的方法来理解一个常见场景中的硬链接,软件安装。
One day I downloaded a software to folder Downloads
for install. After I did sudo make install
, some executables were cp
ed to local bin folder. Here, cp
creates hard link. I was happy with the software but soon realized that Downloads
isn't a good place in the long run. So I mv
ed the software folder to source
directory. Well, I can still run the software as before without worrying about any target link things, like in Windows. This means hard link finds inode directly and other files around.
有一天,我下载了一个软件到文件夹下载安装。在我完成sudo的安装之后,一些可执行文件被cped到本地bin文件夹。在这里,cp创建了硬链接。我对这个软件很满意,但很快意识到下载并不是一个好地方。所以我把软件文件夹放到了源目录中。嗯,我仍然可以像以前一样运行软件,不用担心任何目标链接,比如Windows。这意味着硬链接会直接找到inode和其他文件。
#21
0
IN this answer when i say a file i mean the location in memory
在这个答案中,当我说一个文件时,我指的是内存中的位置。
All the data that is saved is stored in memory using a data structure called inodes Every inode has a inodenumber.The inode number is used to access the inode.All the hard links to a file may have different names but share the same inode number.Since all the hard links have the same inodenumber(which inturn access the same inode),all of them point to the same physical memory.
所有保存的数据都存储在内存中,使用一种称为inode的数据结构,每个inode都有一个inodenumber。inode号用于访问inode。所有与文件的硬链接可能有不同的名称,但共享相同的inode号。因为所有的硬链接都有相同的inodenumber(它可以访问相同的inode),所以它们都指向相同的物理内存。
A symbolic link is a special kind of file.Since it is also a file it will have a file name and an inode number.As said above the inode number acceses an inode which points to data.Now what makes a symbolic link special is that the inodenumbers in symbolic links access those inodes which point to "a path" to another file.More specifically the inode number in symbolic link acceses those inodes who point to another hard link.
符号链接是一种特殊的文件。因为它也是一个文件,它将有一个文件名和一个inode号。如上所述,inode号上有一个指向数据的inode。符号链接的特殊之处在于,符号链接中的inodenumbers访问那些指向另一个文件的“路径”的索引节点。更具体地说,符号链接中的inode号是指那些指向另一个硬链接的inode。
when we are moving,copying,deleting a file in GUI we are playing with the hardlinks of the file not the physical memory.when we delete a file we are deleting the hardlink of the file. we are not wiping out the physical memory.If all the hardlinks to file are deleted then it will not be possible to access the data stored although it may still be present in memory
当我们移动、复制、删除GUI中的文件时,我们使用的是文件的硬链接,而不是物理内存。当我们删除一个文件时,我们正在删除文件的硬链接。我们并没有抹掉物理记忆。如果文件的所有硬链接被删除,那么就不可能访问存储的数据,尽管它可能仍然存在于内存中。
#1
632
Underneath the file system files are represented by inodes (or is it multiple inodes not sure)
文件系统文件的下面是inode(或者是多个inode不确定)
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.
文件系统中的文件基本上是一个到inode的链接。然后,一个硬链接创建另一个文件,并链接到相同的底层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.
当您删除一个文件时,它将删除一个链接到底层的inode。当所有指向inode的链接被删除时,inode只被删除(或可删除/重写)。
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.
一旦一个硬链接被链接到inode。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层的inode。对inode上的数据的任何更改都反映在所有引用该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.
注意:硬链接只在同一个文件系统中有效。符号链接可以跨文件系统,因为它们只是另一个文件的名称。
#2
359
Some nice intuition that might help, using any Linux(ish) console.
使用任何Linux(ish)控制台的一些不错的直觉可能会有所帮助。
Create two files:
创建两个文件:
$ touch blah1; touch blah2
Enter some Data into them:
向他们输入一些数据:
$ echo "Cat" > blah1
$ echo "Dog" > blah2
(Actually, I could have used echo in the first place, as it creates the files if they don't exist... but never mind that.)
(实际上,我可以首先使用echo,因为如果它们不存在,就会创建文件……)但没关系。)
And as expected:
像预期的那样:
$cat blah1; cat blah2
Cat
Dog
Let's create hard and soft links:
让我们创建硬的和软的链接:
$ ln blah1 blah1-hard
$ ln -s blah2 blah2-soft
Let's see what just happened:
让我们看看发生了什么:
$ ls -l
blah1
blah1-hard
blah2
blah2-soft -> blah2
Changing the name of blah1 does not matter:
改变blah1的名称并不重要:
$ mv blah1 blah1-new
$ cat blah1-hard
Cat
blah1-hard points to the inode, the contents, of the file - that wasn't changed.
blah1-hard指向inode,文件的内容,没有改变。
$ mv blah2 blah2-new
$ ls blah2-soft
blah2-soft
$ cat blah2-soft
cat: blah2-soft: No such file or directory
The contents of the file could not be found because the soft link points to the name, that was changed, and not to the contents. Likewise, If blah1 is deleted, blah1-hard still holds the contents; if blah2 is deleted, blah2-soft is just a link to a non-existing file.
无法找到文件的内容,因为软链接指向名称,更改了,而不是内容。同样,如果blah1被删除,blah1-hard仍然保留内容;如果删除了blah2, blah2-soft只是一个与非现有文件的链接。
#3
308
As the saying goes, a picture is worth a thousand words. Here is how I visualize it:
俗话说,一幅画胜过千言万语。下面是我的设想:
Here is how we get to that picture:
下面是我们如何看待这幅图景:
-
Create a name
myfile.txt
in the file system that points to a new inode (which contains the metadata for the file and points to the blocks of data that contain its contents, i.e. the text "Hello, World!":myfile创建一个名称。文件系统中的txt指向一个新的inode(包含文件的元数据并指向包含其内容的数据块,即文本“Hello, World!”)
$ echo 'Hello, World!' > myfile.txt
-
Create a hard link
my-hard-link
to the filemyfile.txt
, which means "create a file that should point to the same inode thatmyfile.txt
points to":创建一个硬链接,硬链接到文件myfile。txt,意思是“创建一个文件,该文件应该指向myfile相同的inode”。txt指向”:
$ ln myfile.txt my-hard-link
-
Create a soft link
my-soft-link
to the filemyfile.txt
, which means "create a file that should point to the filemyfile.txt
":创建一个软链接软链接到文件myfile。txt,意思是“创建一个文件,该文件应该指向文件myfile.txt”:
$ ln -s myfile.txt my-soft-link
Look what will now happen if myfile.txt
is deleted (or moved): my-hard-link
still points to the same contents, and is thus unaffected, whereas my-soft-link
now points to nothing. Other answers discuss the pros/cons of each.
看,如果我的文件。txt被删除(或移动):my-硬链接仍然指向相同的内容,因此不受影响,而my-软链接现在指向什么都没有。其他答案讨论每一个的优点/缺点。
#4
62
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.
当原始文件被移动时,硬链接是有用的。例如,将文件从/bin转移到/usr/bin或到/ usr/local/bin。任何与/bin中的文件的符号链接都将被打破,但是一个硬链接,直接链接到该文件的inode,就不关心了。
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.
硬链接可能占用较少的磁盘空间,因为它们只占用一个目录条目,而symlink需要它自己的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.
硬链接也需要更少的时间来解析——符号链接可以指向在符号链接目录中的其他符号链接。其中一些可能是在NFS或其他高延迟的文件系统上,因此可能导致网络流量的解决。硬链接总是在同一个文件系统上,总是在单个查找中得到解决,而且永远不会涉及网络延迟(如果是NFS文件系统上的硬链接,那么NFS服务器将执行该解析,而客户机系统将看不到它)。有时这是很重要的。对我来说不是这样,但我可以想象出高性能系统的重要性。
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.
我也认为诸如mmap(2)甚至开放(2)使用相同的功能作为硬连接保持文件的inode活跃,这样即使文件被分离(2)ed,inode仍然允许进程继续访问,并只有一次关闭它的文件真的消失。这允许更安全临时文件(如果你能得到的开放和断开自动发生,它可能有一个POSIX API,我不记得,那么你真的有一个安全的临时文件),你可以读/写数据没有人能够访问它。这是真的,之前/proc让每个人都能查看你的文件描述符,但那是另一个故事。
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.
说到这个,恢复一个在进程a中打开的文件,但是在文件系统上没有链接,它围绕着使用硬链接来重新创建inode链接,这样文件在打开或关闭的过程中不会消失。
#5
31
A simple way to see the difference between a hard link and a symbolic link is through a simple example. A hard link to a file will point to the place where the file is stored, or the inode of that file. A symbolic link will point to the actual file itself.
一个简单的方法可以看出硬链接和符号链接之间的区别。文件的硬链接指向存储文件的位置,或该文件的inode。一个符号链接将指向实际的文件本身。
So if we have a file called "a" and create a hard link "b" and a symbolic link "c" which all refer to file "a" :
因此,如果我们有一个名为“a”的文件,并创建一个硬链接“b”和一个符号链接“c”,它们都指向文件“a”:
echo "111" > a
ln a b
ln -s a c
The output of "a", "b", and "c" will be :
“a”、“b”、“c”的输出为:
cat a --> 111
cat b --> 111
cat c --> 111
Now let's remove file "a" and see what happens to the output of "a", "b", and "c":
现在让我们删除文件a,看看“a”、“b”和“c”的输出会发生什么变化:
rm a
cat a --> No such file or directory
cat b --> 111
cat c --> No such file or directory
So what happened?
那么发生了什么?
Because file "c" points to file "a" itself, if file "a" is deleted then file "c" will have nothing to point to, in fact it is also deleted.
因为文件“c”指向文件“a”本身,如果文件“a”被删除,那么文件“c”将没有任何指向,实际上它也被删除了。
However, file "b" points to the place of storage, or the inode, of file "a". So if file "a" is deleted then it will no longer point to the inode, but because file "b" does, the inode will continue to store whatever contents belonged to "a" until no more hard links point to it anymore.
然而,文件“b”指向存储的位置,或文件“a”的inode。因此,如果文件“a”被删除,那么它将不再指向inode,而是因为file“b”做了,inode将继续存储属于“a”的任何内容,直到不再有任何硬链接指向它为止。
#6
28
Soft Link:
软链接:
soft or symbolic is more of a short cut to the original file....if you delete the original the shortcut fails and if you only delete the short cut nothing happens to the original.
软或符号更捷径的原始文件....如果你删除了原始的快捷方式失败,如果你只删除了短切,没有任何事情发生在原始。
Soft link Syntax: ln -s Pathof_Target_file link
软链接语法:ln -s Pathof_Target_file链接。
Output : link -> ./Target_file
输出:link -> ./Target_file。
Proof: readlink link
Also in ls -l link
output you will see the first letter in lrwxrwxrwx
as l which is indication that the file is a soft link.
证明:readlink链接也在ls -l链接输出中,您将在lrwxrwxrwx中看到第一个字母l,表示该文件是一个软链接。
Deleting the link: unlink link
删除链接:取消链接。
Note: If you wish, your softlink can work even after moving it somewhere else from the current dir. Make sure you give absolute path and not relative path while creating a soft link. i.e.(starting from /root/user/Target_file and not ./Target_file)
注意:如果您愿意,您的软链接可以在将它移动到当前目录下的其他地方之后继续工作。确保在创建软链接时提供绝对路径,而不是相对路径。即。(从/ root/user/target_file开始,而不是。/Target_file)
Hard Link:
硬链接:
Hard link is more of a mirror copy or multiple paths to the same file. Do something to file1 and it appears in file 2. Deleting one still keeps the other ok.
硬链接更多的是一个镜像拷贝或多个路径到同一个文件。对file1做一些事情,它会出现在文件2中。删除一个仍然保留另一个ok。
The inode(or file) is only deleted when all the (hard)links or all the paths to the (same file)inode has been deleted.
当所有(硬)链接或所有(相同文件)inode的路径被删除时,inode(或文件)才会被删除。
Once a hard link has been made the link has the inode of the original file. 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.
一旦一个硬链接被建立,链接就有了原始文件的索引。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层的inode。对inode上的数据的任何更改都反映在所有引用该inode的文件中。
Hard Link syntax: ln Target_file link
硬链接语法:ln Target_file链接。
Output: A file with name link will be created with the same inode number as of Targetfile.
输出:将使用与Targetfile相同的inode号创建一个带有名称链接的文件。
Proof: ls -i link Target_file
(check their inodes)
证明:ls -i链接Target_file(检查它们的inode)
Deleting the link: rm -f link
(Delete the link just like a normal file)
删除链接:rm -f链接(删除链接,就像普通文件一样)
Note: Symbolic links can span file systems as they are simply the name of another file. Whereas hard links are only valid within the same File System.
注意:符号链接可以跨文件系统,因为它们只是另一个文件的名称。而硬链接只在同一个文件系统中有效。
Symbolic links have some features hard links are missing:
符号链接有一些硬链接缺失的特征:
- 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.
- 硬链接共享相同的inode。软链接。
- 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.
- 您知道,当一个符号链接指向硬链接时,您需要探索整个文件系统来查找共享相同inode的文件。
- hard-links cannot point to directories.
- 硬链接不能指向目录。
Reason why hard-links can't cross file systems or partitions:
为什么硬链接不能跨文件系统或分区:
On a hard disk there are lots of sectors.
在硬盘上有很多扇区。
Say a file starts at inode (sector) 4001 and ends at 5000. The file is "/export/home/john/mail.doc"
说一个文件从inode(扇区)4001开始,到5000结束。该文件“/出口/ home /约翰/ mail.doc”
Then: 1. A hard link to "mail.doc" which is named "hardLinkToMail" contains the value: "4001". 2. A soft link to "mail.doc" which is named "softLinkToMail" contains the value: "/export/home/john/mail.doc".
:1。硬链接到“邮件”。名为“hardLinkToMail”的文档包含了“4001”的值。2。软链接到“邮件”。“doc”是“softLinkToMail”,包含“/export/home/john/mail.doc”的价值。
In 1) the hard link can only point to the same disk. It can not point to another drive. All drives has an inode of value "4001", how can the hard link distinguish between all discs? Which drive's "4001" is it?
在1)硬链接只能指向同一个磁盘。它不能指向另一个驱动器。所有驱动器都有一个值“4001”,硬链接如何区分所有磁盘?哪一个驱动器的“4001”是它?
In 2) the soft link contains a string. The string can point to another filesystem on another drive, because the full path is specified.
在2)软链接包含一个字符串。该字符串可以指向另一个驱动器上的另一个文件系统,因为指定了完整的路径。
#7
26
Symbolic links link to a path name. This can be anywhere in a system's file tree, and doesn't even have to exist when the link is created. The target path can be relative or absolute.
符号链接指向路径名。这可以是系统文件树中的任何地方,而且在创建链接时甚至不需要存在。目标路径可以是相对的,也可以是绝对的。
Hard links are additional pointers to an inode, meaning they can exist only on the same volume as the target. Additional hard links to a file are indistinguishable from the "original" name used to reference a file.
硬链接是指向inode的额外指针,这意味着它们只能以与目标相同的音量存在。对文件的附加硬链接与引用文件的“原始”名称难以区分。
#8
18
I would point you to Wikipedia:
我想指出*:
- Symbolic link
- 符号链接
- Hard link
- 硬链接
A few points:
几个点:
- Symlinks, unlike hard links, can cross filesystems (most of the time).
- 与硬链接不同,符号链接可以跨文件系统(大部分时间)。
- Symlinks can point to directories.
- 符号链接可以指向目录。
- Hard links point to a file and enable you to refer to the same file with more than one name.
- 硬链接指向一个文件,使您可以使用多个名称引用相同的文件。
- As long as there is at least one link, the data is still available.
- 只要有至少一个链接,数据仍然可用。
#9
8
Hard links are very useful when doing incremental backups. See rsnapshot, for example. The idea is to do copy using hard links:
在进行增量备份时,硬链接非常有用。例如,看到rsnapshot。这个想法是用硬链接复制:
- copy backup number n to n + 1
- 复制备份号码n到n + 1。
- copy backup n - 1 to n
- 复制备份n - 1到n。
- ...
- …
- copy backup 0 to backup 1
- 复制备份0到备份1。
- update backup 0 with any changed files.
- 更新备份0与任何更改的文件。
The new backup will not take up any extra space apart from any changes you've made, since all the incremental backups will point to the same set of inodes for files which haven't changed.
新的备份不会占用您所做的任何更改之外的任何额外空间,因为所有增量备份都将指向相同的文件集,而这些文件没有改变。
#10
5
Hard links can be useful, because it enables you to access the file from multiple different locations. it access inode directly. Some restrictions are :
硬链接可能是有用的,因为它使您能够从多个不同的位置访问文件。它直接访问inode。一些限制是:
- Hard links must exist all on the same device.
- 硬链接必须存在于同一设备上。
- We cannot create hard links to directories.
- 我们不能为目录创建硬链接。
- The number of aliases the original file has. When the last name is removed, the contents are also removed.
- 原始文件的别名数目。当删除最后一个名称时,内容也会被删除。
The nice thing about hard links is that no difference exists between the first hard link and the second hard link. They are both just hard links, and if the first hard link that ever existed for a file is removed, that does not impact the other hard links that still exist. The Linux operating system uses links on many locations to make files more accessible.
硬链接的优点在于,第一个硬链接和第二个硬链接之间没有区别。它们都是硬链接,如果文件的第一个硬链接被删除,它不会影响到其他仍然存在的硬链接。Linux操作系统在许多地方使用链接,使文件更容易访问。
Where as a symbolic link (also referred to as soft link) does not link directly to the inode but to the name of the file.The major disadvantage is that when the original file is removed, the symbolic link becomes invalid and does not work any longer.
作为一个符号链接(也称为软链接),它不直接链接到inode,而是链接到文件的名称。主要缺点是当删除原始文件时,符号链接无效,不再工作。
Some information on Inode:
一些索引节点信息:
Linux stores administrative data about files in inodes. Every file on Linux has an inode, and in the inode, important information about the file is stored:
Linux存储关于inode文件的管理数据。Linux上的每个文件都有一个inode,在inode中存储了关于文件的重要信息:
- The data block where the file contents are stored
- 存储文件内容的数据块。
- The creation, access, and modification date
- 创建、访问和修改日期。
- Permissions
- 权限
- File owners
- 文件所有者
Just one important piece of information is not stored in the inode: the name. Names are stored in the directory, and each filename knows which inode it has to address to access further file information. It is interesting to know that an inode does not know which name it has; it just knows how many names are associated with the inode. These names are referred to as hard links. When you create a file, you give it a name. Basically, this name is a hard link.
只有一个重要的信息没有存储在inode中:名称。名称存储在目录中,每个文件名知道它必须处理哪些inode以访问进一步的文件信息。很有趣的是,一个inode不知道它的名字;它只知道有多少个名称与inode相关联。这些名称被称为硬链接。当你创建一个文件,你给它一个名字。基本上,这个名字是一个硬链接。
#11
4
I add on Nick's question: when are hard links useful or necessary? The only application that comes to my mind, in which symbolic links wouldn't do the job, is providing a copy of a system file in a chrooted environment.
我补充了尼克的问题:什么时候硬链接有用或必要?在我的脑海中,只有符号链接不能完成工作的应用程序,在一个有根的环境中提供了一个系统文件的副本。
#12
3
Also:
另外:
- Read performance of hard links is better than symbolic links (micro-performance)
- 读取硬链接的性能优于符号链接(微性能)
- Symbolic links can be copied, version-controlled, ..etc. In another words, they are an actual file. On the other end, a hard link is something at a slightly lower level and you will find that compared to symbolic links, there are less tools that provide means for working with the hard links as hard links and not as normal files
- 符号链接可以复制,版本控制,等等。换句话说,它们是一个实际的文件。在另一端,硬链接的级别稍低一些,您会发现与符号链接相比,有更少的工具提供了与硬链接一起工作的方法,这是硬链接,而不是普通文件。
#13
3
What you think of as an ordinary "file" is actually two separate things: The data of a file, and a directory entry. When you create a hard link for a file, you actually create a second directory entry which refers to the same data. Both directory entries have the exact same functionality; each one can be used to open the file to read it. So you don't really have "a file plus a hard link", you have "file data with two directory entries". What you think of as deleting a file actually deletes a directory entry, and when the last directory entry for the data is deleted, then the data itself is deleted as well. For ordinary files that have only one directory entry, deleting the directory entry will delete the data as always. (While a file is opened, the OS creates a temporary link to the file, so even when you delete all directory entries, the data stays but disappears as soon as you close the file).
您所认为的普通“文件”实际上是两个独立的东西:一个文件的数据和一个目录条目。当您为一个文件创建一个硬链接时,您实际上创建了第二个目录条目,该条目引用相同的数据。两个目录项都具有完全相同的功能;每个都可以用来打开文件来读取它。所以你没有“一个文件加上一个硬链接”,你有“两个目录条目的文件数据”。您认为删除一个文件实际上删除了一个目录条目,当数据的最后一个目录条目被删除时,数据本身也会被删除。对于只有一个目录条目的普通文件,删除目录项将始终删除数据。(当一个文件被打开时,操作系统会创建一个临时的文件链接,所以即使你删除了所有的目录条目,数据也会保留,但是一旦你关闭了文件,它就会消失)。
As an example, create a file A.txt, a hard link B.txt, and delete A.txt. When you created A.txt, some data was created, and a directory entry A.txt. When you created the hard link, another directory entry B.txt was created, pointing to the exact same data. When you delete A.txt, you still have all the data and a single directory entry B.txt, exactly as if you had create a file B.txt in the first place.
例如,创建一个文件a。txt,一个硬链接B。txt和删除A.txt。当你创建了一个。txt,一些数据被创建,一个目录条目A.txt。当您创建硬链接时,另一个目录条目B。txt被创建,指向完全相同的数据。当你删除一个。txt,你仍然有所有的数据和一个目录条目B。txt,就好像你创建了一个文件B。首先是txt。
A soft link is just an (almost) ordinary file, except that it doesn't contain data, but the path of another directory entry. If you delete the file that the soft link refers to, then the soft link will contain a path that doesn't point to a directory entry anymore; it is broken. If you delete the soft link, it's like deleting any other file, the file it points to is unaffected.
软链接只是一个(几乎)普通的文件,只是它不包含数据,而是另一个目录条目的路径。如果删除软链接引用的文件,那么软链接将包含一条路径,不再指向目录条目;它坏了。如果删除软链接,就像删除其他文件一样,它指向的文件不会受到影响。
#14
2
Simply , Hard link : is just add new name to a file, that's mean , a file can have many name in the same time, all name are equal to each other, no one preferred, Hard link is not mean to copy the all contents of file and make new file is not that, it just create an alternative name to be known..
简单,硬链接:只是新名称添加到一个文件,这是说,一个文件可以在同一时间有很多的名字,名字都是相等的,没有人喜欢,硬链接并不意味着复制文件的所有内容,使新文件不是,它只是创建另一个名字叫. .
Symbolic link (symlink) : is a file pointer to another file, if the symbolic link points to an existing file which is later deleted, the symbolic link continues to point to the same file name even though the name no longer names any file.
符号链接(symlink):是指向另一个文件的文件指针,如果符号链接指向一个已存在的文件,该文件随后被删除,符号链接仍然指向相同的文件名,即使该名称不再命名任何文件。
#15
2
From MSDN,
Symbolic link
A symbolic link is a file-system object that points to another file system object. The object being pointed to is called the target.
符号链接是指向另一个文件系统对象的文件系统对象。被指向的对象称为目标。
Symbolic links are transparent to users; the links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner.
符号链接对用户来说是透明的;链接显示为正常的文件或目录,并且可以由用户或应用程序以完全相同的方式进行操作。
Symbolic links are designed to aid in migration and application compatibility with UNIX operating systems. Microsoft has implemented its symbolic links to function just like UNIX links.
符号链接旨在帮助迁移和应用程序兼容UNIX操作系统。微软已经实现了它的符号链接,就像UNIX链接一样。
Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name; relative links are determined relative to where relative–link specifiers are in a specified path
符号链接可以是绝对链接,也可以是相对链接。绝对链接是指定路径名的每一部分的链接;相对链接是确定的,相对链接说明符在指定的路径中。
An example of Absolute Symbolic Link
一个绝对符号链接的例子。
X: "C:\alpha\beta\absLink\gamma\file"
Link: "absLink" maps to "\\machineB\share"
Modified Path: "\\machineB\share\gamma\file"
An example of Relative Symbolic Links
一个相对符号链接的例子。
X: C:\alpha\beta\link\gamma\file
Link: "link" maps to "..\..\theta"
Modified Path: "C:\alpha\beta\..\..\theta\gamma\file"
Final Path: "C:\theta\gamma\file"
Hard link
A hard link is the file system representation of a file by which more than one path references a single file in the same volume.
硬链接是一个文件的文件系统表示,该文件的多个路径引用同一卷中的单个文件。
To create a hard link in windows, navigate to where link is to be created and enter this command:
要在windows中创建一个硬链接,导航到要创建链接的地方,并输入以下命令:
mklink /H Link_name target_path
Note that you can delete hard links any order, regardless of the order in which they were created. Also, hard links can not be created when
请注意,您可以删除硬链接任何顺序,不管它们创建的顺序是什么。同时,也不能创建硬链接。
- references are in different local drives
- 引用位于不同的本地驱动器中。
- references include network drive. In other words, one of the references is a network drive
- 引用包括网络驱动器。换句话说,其中一个引用是网络驱动器。
- hard link to be created is in the same path as the target
- 要创建的硬链接与目标的路径相同。
Junction
NTFS supports another link type called junction. MSDN defines it as follows:
NTFS支持另一种称为连接的链接类型。MSDN将其定义如下:
A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links.
一个连接(也称为软链接)不同于硬链接,因为它引用的存储对象是单独的目录,一个连接可以链接位于同一计算机上不同本地卷上的目录。否则,连接操作与硬链接相同。
The bolded parts in hard link section and junction section show the basic difference between the two.
硬连接部分和连接部分的粗体部分显示了两者之间的基本区别。
Command to create a junction in windows, navigate to where link is to be created and then enter:
命令在windows中创建一个连接,导航到要创建链接的地方,然后输入:
mklink /J link_name target_path
#16
2
A directory entry is link a structrue:
一个目录条目链接一个结构:
struct dentry{
ino_t ino;
char name[256];
}
the ino is the number of inode, the name is the file name, inode structure maybe like:
伊诺是索引节点的数量,名字是文件名,inode结构可能就像:
struct inode{
link_t nlink;
...
}
for example you creat a file /1, the directory entry maybe like:
例如,您创建了一个文件/1,目录条目可能如下:
struct dentry{
ino_t ino; /* such as 15 */
char name[256]; /* "1" */
}
the inode struct maybe like:
inode结构可能是这样的:
struct inode{ /* inode number 15 */
link_t nlink; /* nlink = 1 */
...
}
then you create a hard link(may be /100), the directory entry maybe like:
然后创建一个硬链接(可能是/100),目录条目可能是:
struct dentry{
ino_t ino; /* 15 */
char name[256]; /* 100 */
}
the inode struct maybe like:
inode结构可能是这样的:
struct inode{ /* inode numebr 15 */
link_t nlink; /* nlink = 2 */
...
}
then you create a symbolic link(may be /200) to file 1, the directory entry maybe like:
然后创建一个符号链接(可能是/200)到文件1,目录条目可能是:
struct dentry{
ino_t ino; /* such as 16 */
char name[256]; /* "200" */
}
the inode struct maybe like:
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 */
#17
2
Adding to all the above answers, the difference in finding the hardlink and softlink file can be understood as below:
在以上所有的答案中,找到硬链接和软链接文件的区别可以理解如下:
I have a file f6
in my current directory, as well as a directory named t2
.
我在当前目录中有一个文件f6,以及一个名为t2的目录。
File named f1
and ./t2/f2
are symbolic links to f6
.
文件命名为f1和。/t2/f2是f6的符号链接。
File named f7
and ./t2/f8
are hard links of f6
.
文件名f7和/t2/f8是f6的硬链接。
To find soft as well as hard link we can use:
要找到软的和硬的链接,我们可以使用:
$ find -L . -samefile f6
> ./f1
> ./f6
> ./f7
> ./t2/f2
> ./t2/f8
To find only hardlink we can use:
找到我们可以使用的硬链接:
$ find . -xdev -samefile f6
> ./f6
> ./f7
> ./t2/f8
Since hardlinks can be created on the same file system, we can search all the hardlinks without -L
option used (with -xdev
option) in the same file-system/mount-point. It saves the unnecessary search into different mount points.
由于可以在同一个文件系统上创建硬链接,所以我们可以在相同的文件系统/安装点中搜索所有的硬链接,而不使用-L选项(和-xdev选项)。它将不必要的搜索保存到不同的挂载点。
So searching the hardlink is somewhat faster then searching the softlinks(Please rectify if I am wrong or not clear).
所以搜索硬链接要比搜索软链接快一些(如果我错了或者不清楚,请纠正)。
#18
#19
1
Symbolic links give another name to a file, in a way similar to hard links. But a file can be deleted even if there are remaining symbolic links.
符号链接以类似于硬链接的方式给文件提供另一个名称。但即使存在符号链接,也可以删除文件。
#20
0
I just found an easy way to understand hard links in a common scenario, software install.
我只是找到了一种简单的方法来理解一个常见场景中的硬链接,软件安装。
One day I downloaded a software to folder Downloads
for install. After I did sudo make install
, some executables were cp
ed to local bin folder. Here, cp
creates hard link. I was happy with the software but soon realized that Downloads
isn't a good place in the long run. So I mv
ed the software folder to source
directory. Well, I can still run the software as before without worrying about any target link things, like in Windows. This means hard link finds inode directly and other files around.
有一天,我下载了一个软件到文件夹下载安装。在我完成sudo的安装之后,一些可执行文件被cped到本地bin文件夹。在这里,cp创建了硬链接。我对这个软件很满意,但很快意识到下载并不是一个好地方。所以我把软件文件夹放到了源目录中。嗯,我仍然可以像以前一样运行软件,不用担心任何目标链接,比如Windows。这意味着硬链接会直接找到inode和其他文件。
#21
0
IN this answer when i say a file i mean the location in memory
在这个答案中,当我说一个文件时,我指的是内存中的位置。
All the data that is saved is stored in memory using a data structure called inodes Every inode has a inodenumber.The inode number is used to access the inode.All the hard links to a file may have different names but share the same inode number.Since all the hard links have the same inodenumber(which inturn access the same inode),all of them point to the same physical memory.
所有保存的数据都存储在内存中,使用一种称为inode的数据结构,每个inode都有一个inodenumber。inode号用于访问inode。所有与文件的硬链接可能有不同的名称,但共享相同的inode号。因为所有的硬链接都有相同的inodenumber(它可以访问相同的inode),所以它们都指向相同的物理内存。
A symbolic link is a special kind of file.Since it is also a file it will have a file name and an inode number.As said above the inode number acceses an inode which points to data.Now what makes a symbolic link special is that the inodenumbers in symbolic links access those inodes which point to "a path" to another file.More specifically the inode number in symbolic link acceses those inodes who point to another hard link.
符号链接是一种特殊的文件。因为它也是一个文件,它将有一个文件名和一个inode号。如上所述,inode号上有一个指向数据的inode。符号链接的特殊之处在于,符号链接中的inodenumbers访问那些指向另一个文件的“路径”的索引节点。更具体地说,符号链接中的inode号是指那些指向另一个硬链接的inode。
when we are moving,copying,deleting a file in GUI we are playing with the hardlinks of the file not the physical memory.when we delete a file we are deleting the hardlink of the file. we are not wiping out the physical memory.If all the hardlinks to file are deleted then it will not be possible to access the data stored although it may still be present in memory
当我们移动、复制、删除GUI中的文件时,我们使用的是文件的硬链接,而不是物理内存。当我们删除一个文件时,我们正在删除文件的硬链接。我们并没有抹掉物理记忆。如果文件的所有硬链接被删除,那么就不可能访问存储的数据,尽管它可能仍然存在于内存中。