符号链接和硬链接有什么区别?
最近我在面试时被问到这个问题。 我很诚实地说我知道符号链接的行为方式以及如何创建符号链接,但不了解硬链接的使用以及它与符号链接的区别。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
在文件系统下面,文件由索引节点表示。 (或者是多个 inode?不确定。)
文件系统中的文件基本上是到 inode 的链接。
那么,硬链接只是创建另一个文件,其中包含指向同一底层 inode 的链接。
当您删除文件时,它会删除指向底层索引节点的一个链接。 仅当指向该索引节点的所有链接都已删除时,该索引节点才会被删除(或可删除/可重写)。
符号链接是到文件系统中另一个名称的链接。
一旦建立了硬链接,该链接就会指向 inode。 删除、重命名或移动原始文件不会影响硬链接,因为它链接到底层 inode。 对 inode 上数据的任何更改都会反映在引用该 inode 的所有文件中。
注意:硬链接仅在同一文件系统内有效。 符号链接可以跨越文件系统,因为它们只是另一个文件的名称。
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.
俗话说,一图胜千言。 以下是我如何将其可视化:
以下是我们如何获取该图片:
创建名称文件系统中的
myfile.txt
指向一个新的 inode(其中包含文件的元数据并指向包含其内容的数据块,即文本“Hello, World!”):创建到文件
myfile.txt
的硬链接my-hard-link
,这意味着“创建一个文件应该指向myfile.txt
指向的同一个 inode":创建指向文件
myfile.txt
的软链接my-soft-link
,其中意思是“创建一个应该指向文件myfile.txt
的文件”:看看如果删除(或移动)
myfile.txt
会发生什么:my-hard-link
仍然指向相同的内容,因此不受影响,而my-soft-link
现在不指向任何内容。 其他答案讨论了每种方法的优缺点。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!":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":Create a soft link
my-soft-link
to the filemyfile.txt
, which means "create a file that should point to the filemyfile.txt
":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, whereasmy-soft-link
now points to nothing. Other answers discuss the pros/cons of each.一些可能有帮助的例子。
创建两个包含数据的文件:
创建硬链接和软链接(也称为符号链接):
通过增加大小以长格式列出目录内容:
这告诉我们
第一列:软链接和硬链接的文件模式不同< /p>
lrwxr-xr-x
l
= 符号链接rwx
= 可读、可写、可执行rx
= 可读、不可写、可执行rx
= 可读、不可写、可执行-rw-r--r--
-
= 常规文件rw-
= 可读、可写、不可执行r--
= 可读、不可写、不可执行r--
= 可读、不可写、不可执行第二列:硬链接文件的链接数较高
第五列:软链接的大小较小,因为它是引用而不是副本
最后一列:符号链接通过
->
更改 foo 的文件名不会影响 foo-hard:
更改 foo 的内容反映在 foo-hard:
硬链接 中foo-hard 指向文件的 inode、内容。
对于像 bar-soft 这样的软链接来说,情况并非如此:
无法找到文件的内容,因为软链接指向已更改的名称,而不是内容。
同样,如果
foo
被删除,foo-hard
仍然保留内容; 如果bar
被删除,bar-soft
只是一个指向不存在文件的链接。Some examples that might help.
Create two files with data in them:
Create a hard and soft (aka symbolic) link:
List directory contents in long format by increasing size:
This tell us that
1st column: the file mode for the soft and hard links differ
lrwxr-xr-x
l
= symbolic linkrwx
= readable, writable, executabler-x
= readable, not writable, executabler-x
= readable, not writable, executable-rw-r--r--
-
= regular filerw-
= readable, writable, not executabler--
= readable, not writable, not executabler--
= readable, not writable, not executable2nd column: number of links is higher for the hard linked files
5th column: the size of the soft link is smaller, because it's a reference as opposed to a copy
last column: the symbolic link shows the linked-to file via
->
Changing the filename of foo does not affect foo-hard:
Changing the contents of foo is reflected in foo-hard:
Hard links like foo-hard point to the inode, the contents, of the file.
This is not the case for soft links like bar-soft:
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
foo
is deleted,foo-hard
still holds the contents; ifbar
is deleted,bar-soft
is just a link to a non-existing file.当原始文件被移动时,硬链接非常有用。 例如,将文件从 /bin 移动到 /usr/bin 或 /usr/local/bin。 /bin 中文件的任何符号链接都会被破坏,但硬链接(直接链接到文件的索引节点)不会在意。
硬链接可能占用更少的磁盘空间,因为它们只占用一个目录条目,而符号链接需要它自己的索引节点来存储它指向的名称。
硬链接也需要更少的时间来解析 - 符号链接可以指向符号链接目录中的其他符号链接。 其中一些可能位于 NFS 或其他高延迟文件系统上,因此可能会导致网络流量需要解决。 硬链接始终位于同一文件系统上,始终在单次查找中解析,并且永远不会涉及网络延迟(如果它是 NFS 文件系统上的硬链接,则 NFS 服务器将进行解析,并且对于其他人来说是不可见的)客户端系统)。 有时这很重要。 不适合我,但我可以想象高性能系统中这一点可能很重要。
我还认为像 mmap(2) 甚至 open(2) 这样的东西使用与硬链接相同的功能来保持文件的 inode 活动,这样即使文件被 unlink(2)ed,inode 仍然允许进程继续访问,只有当进程关闭时,文件才会真正消失。 这允许更安全的临时文件(如果你可以原子地进行打开和取消链接,可能有一个我不记得的 POSIX API,那么你真的有一个安全的临时文件),你可以在其中读/写您的数据,任何人都无法访问它。 嗯,在 /proc 让每个人都能够查看文件描述符之前确实如此,但那是另一个故事了。
说到这里,恢复在进程 A 中打开但在文件系统上未链接的文件涉及使用硬链接来重新创建 inode 链接,这样当打开该文件的进程关闭该文件或消失时,该文件就不会消失。
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.
了解硬链接和符号链接之间区别的简单方法是通过一个简单的示例。 文件的硬链接将指向文件存储的位置,或该文件的索引节点。 符号链接将指向实际文件本身。
因此,如果我们有一个名为“a”的文件并创建一个硬链接“b”和一个符号链接“c”,它们都引用文件“a”:
“a”、“b”和“c”的输出将be :
现在让我们删除文件“a”,看看“a”、“b”和“c”的输出会发生什么:
那么发生了什么?
因为文件“c”指向文件“a”本身,所以如果文件“a”被删除,那么文件“c”将没有任何指向的东西,实际上它也被删除了。
然而,文件“b”指向文件“a”的存储位置或索引节点。 因此,如果文件“a”被删除,那么它将不再指向该索引节点,但由于文件“b”指向该索引节点,索引节点将继续存储属于“a”的任何内容,直到不再有硬链接指向它。
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.
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" :
The output of "a", "b", and "c" will be :
Now let's remove file "a" and see what happens to the output of "a", "b", and "c":
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.
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.
软链接:
软链接或符号链接更多的是原始文件的快捷方式......如果删除原始文件,快捷方式将失败,而如果您只删除快捷方式原版没有任何变化。
软链接语法:
ln -s Pathof_Target_file link
输出:
link -> ./Target_file
证明:
readlink 链接
另外,在 ls -l link 输出中,您将看到 lrwxrwxrwx 中的第一个字母为 l,这表明该文件是软链接。
删除链接:
取消链接
注意:如果您愿意,您的软链接即使在将其从当前目录移动到其他位置后也可以工作。 确保在创建软链接时提供绝对路径而不是相对路径。 即(从 /root/user/Target_file 开始,而不是 ./Target_file)
硬链接:
硬链接更多的是同一文件的镜像副本或多个路径。 对 file1 执行一些操作,它会出现在文件 2 中。
删除一个仍然可以保持另一个正常。
仅当删除了(同一文件)inode 的所有(硬)链接或所有路径时,才删除inode(或文件)。
一旦建立了硬链接,该链接就具有原始文件的索引节点。 删除重命名或移动原始文件不会影响硬链接,因为它链接到底层 inode。 对 inode 上数据的任何更改都会反映在引用该 inode 的所有文件中。
硬链接语法:
ln Target_file link
输出: 将创建一个名为 link 的文件,其 inode 编号与 Targetfile 相同。
证明:
ls -i link Target_file
(检查其 inode)删除链接:
rm -f link
(删除链接就像普通文件一样)注意:符号链接可以跨文件系统,因为它们只是另一个文件的名称。 而硬链接仅在同一文件系统内有效。符号链接具有硬链接所缺少的一些功能:
文件名。
具有文件名大小。
当使用硬链接时,你立即知道符号链接指向哪里
链接,您需要探索整个文件系统才能找到文件
共享相同的索引节点。
# find / -inum 517333
<块引用>
硬链接不能指向目录。
硬链接有两个限制:
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
Output :
link -> ./Target_file
Proof:
readlink link
Also in
ls -l link
output you will see the first letter inlrwxrwxrwx
as l which is indication that the file is a soft link.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)
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.
The inode(or file) is only deleted when all the (hard)links or all the paths to the (same file)inode has been deleted.
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.
Hard Link syntax:
ln Target_file link
Output: A file with name link will be created with the same inode number as of Targetfile.
Proof:
ls -i link Target_file
(check their inodes)Deleting the link:
rm -f link
(Delete the link just like a normal file)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:
file name.
having the file name size.
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
hard-links cannot point to directories.
The hard links have two limitations:
符号链接链接到路径名。 它可以位于系统文件树中的任何位置,甚至在创建链接时不必存在。 目标路径可以是相对路径或绝对路径。
硬链接是指向 inode 的附加指针,这意味着它们只能存在于与目标相同的卷上。 文件的附加硬链接与用于引用文件的“原始”名称无法区分。
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.
我建议您访问维基百科:
几点:
I would point you to Wikipedia:
A few points:
硬链接与软链接可以通过此图片轻松解释。
Hard link Vs Soft link can be easily explained by this image.
进行增量备份时,硬链接非常有用。 例如,请参阅 rsnapshot。 这个想法是使用硬链接进行复制:
除了您所做的任何更改之外,新备份不会占用任何额外空间,因为所有增量备份都将指向未更改文件的同一组索引节点。
Hard links are very useful when doing incremental backups. See rsnapshot, for example. The idea is to do copy using hard links:
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.
来自 MSDN,
< a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365680(v=vs.85).aspx" rel="noreferrer">符号链接
绝对符号链接示例
相对符号链接示例
硬链接
要在 Windows 中创建硬链接,请导航到要创建链接的位置并输入以下命令:
请注意,您可以按任意顺序删除硬链接,无论它们的创建顺序如何。 时,无法创建硬链接
连接
NTFS 支持另一种称为连接的链接类型。 MSDN对其的定义如下:
硬链接部分和连接部分中的粗体部分显示了两者之间的基本区别。
在 Windows 中创建连接的命令,导航到要创建链接的位置,然后输入:
From MSDN,
Symbolic link
An example of Absolute Symbolic Link
An example of Relative Symbolic Links
Hard link
To create a hard link in windows, navigate to where link is to be created and enter this command:
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
Junction
NTFS supports another link type called junction. MSDN defines it as follows:
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:
这个答案适用于 Web 开发人员
This answers is for Web Developers ????:
Hard Link: like pointing different domain names to the same host
Sym Link : redirecting one domain to another domain
简单来说,硬链接:只是给一个文件添加新的名称,也就是说,一个文件可以同时有多个名称,所有名称都相同,没有谁优先,硬链接并不意味着复制所有内容 则
符号链接(symlink):是一个指向另一个文件的文件指针,如果符号链接指向一个现有的文件并且后来被删除, 符号链接继续指向相同的文件名,即使该名称不再命名任何文件。
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.
我对用法的两点意见:
软链接可用于缩短长路径名,即:
对
/short/file.txt
所做的更改将应用于原始文件。硬链接可用于移动大文件:
ln /myapp/dev/application.bin /myapp/prd/application.bin
即时复制到不同的文件夹和原始文件夹文件(位于
/myapp/dev
上)可以移动或删除,而无需触及/myapp/prd
上的文件My two cents on usage:
Soft links may be used to shorten long path names, i.e.:
Changes made to
/short/file.txt
will be applied on the original file.Hard links may be used to move around big files:
ln /myapp/dev/application.bin /myapp/prd/application.bin
Instant copy to different folder, and original file (on
/myapp/dev
) may be moved or deleted, without touching the file on/myapp/prd
另外:
Also:
您所认为的普通“文件”实际上是两个独立的东西:文件的数据和目录条目。 当您为文件创建硬链接时,您实际上创建了引用相同数据的第二个目录条目。 两个目录条目具有完全相同的功能; 每一个都可以用来打开文件来读取它。 所以你实际上并没有“一个文件加一个硬链接”,你有“带有两个目录条目的文件数据”。 您所认为的删除文件实际上会删除一个目录条目,当删除数据的最后一个目录条目时,数据本身也会被删除。 对于只有一个目录项的普通文件,删除该目录项也会一如既往地删除数据。 (打开文件时,操作系统会创建到该文件的临时链接,因此即使您删除所有目录条目,数据也会保留,但一旦关闭文件就会消失)。
例如,创建文件A.txt,硬链接B.txt,并删除A.txt。 当您创建 A.txt 时,会创建一些数据和一个目录条目 A.txt。 创建硬链接时,会创建另一个目录条目 B.txt,指向完全相同的数据。 当您删除 A.txt 时,您仍然拥有所有数据和单个目录条目 B.txt,就像您首先创建了文件 B.txt 一样。
软链接只是一个(几乎)普通文件,只不过它不包含数据,而是另一个目录项的路径。 如果删除软链接引用的文件,那么软链接将包含不再指向目录项的路径; 它被打破。 如果删除软链接,就像删除任何其他文件一样,它指向的文件不受影响。
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 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.
目录项链接一个结构体:
ino是inode的编号,name是文件名,inode结构可能是这样的:
比如你创建了一个文件/1,目录项可能是这样的:
inode结构体可能是这样的:
then您创建一个硬链接(可能是 /100),目录项可能像:
inode 结构可能像:
然后您创建一个到文件 1 的符号链接(可能是 /200),目录项可能像:
inode 结构可能像:喜欢:
A directory entry is link a structrue:
the ino is the number of inode, the name is the file name, inode structure maybe like:
for example you creat a file /1, the directory entry maybe like:
the inode struct maybe like:
then you create a hard link(may be /100), the directory entry maybe like:
the inode struct maybe like:
then you create a symbolic link(may be /200) to file 1, the directory entry maybe like:
the inode struct maybe like:
添加到上述所有答案,查找硬链接和软链接文件的区别可以理解如下:
我当前目录中有一个文件
f6
,以及一个名为t2< 的目录/代码>。
名为
f1
和./t2/f2
的文件是指向f6
的符号链接。名为
f7
和./t2/f8
的文件是f6
的硬链接。要查找软链接和硬链接,我们可以使用:
要仅查找硬链接,我们可以使用:
由于可以在同一文件系统上创建硬链接,因此我们可以在不使用
-L
选项的情况下搜索所有硬链接(使用-xdev
选项)在同一文件系统/挂载点中。 它节省了对不同安装点的不必要的搜索。因此,搜索硬链接比搜索软链接要快一些(如果我错了或不清楚,请纠正)。
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 namedt2
.File named
f1
and./t2/f2
are symbolic links tof6
.File named
f7
and./t2/f8
are hard links off6
.To find soft as well as hard link we can use:
To find only hardlink we can use:
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.So searching the hardlink is somewhat faster then searching the softlinks(Please rectify if I am wrong or not clear).
符号链接为文件提供另一个名称,其方式类似于硬链接。 但即使存在剩余的符号链接,也可以删除文件。
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.
我刚刚找到了一种简单的方法来理解常见场景(软件安装)中的硬链接。
有一天,我将一个软件下载到文件夹
Downloads
中进行安装。 在我执行 sudo make install 后,一些可执行文件被 cp 到本地 bin 文件夹。 此处,cp
创建硬链接。 我对这个软件很满意,但很快意识到从长远来看下载
并不是一个好地方。 因此,我将软件文件夹mv
编辑到source
目录。 好吧,我仍然可以像以前一样运行该软件,而不必担心任何目标链接问题,就像在 Windows 中一样。 这意味着硬链接直接找到inode和周围的其他文件。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 didsudo make install
, some executables werecp
ed to local bin folder. Here,cp
creates hard link. I was happy with the software but soon realized thatDownloads
isn't a good place in the long run. So Imv
ed the software folder tosource
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.在这个答案中,当我说文件时,我的意思是内存中的位置
所有保存的数据都使用称为索引节点的数据结构存储在内存中每个索引节点都有一个索引节点号。索引节点号用于访问索引节点。所有硬链接一个文件可能有不同的名称,但共享相同的 inode 号。由于所有硬链接都具有相同的 inode 号(进而访问相同的 inode),因此它们都指向相同的物理内存。
符号链接是一种特殊的文件。因为它也是一个文件,所以它有一个文件名和一个索引节点号。如上所述,索引节点号访问指向数据的索引节点。现在符号链接的特殊之处在于:符号链接中的索引节点号访问指向另一个文件的“路径”的索引节点。更具体地说,符号链接中的索引节点号访问指向另一个硬链接的索引节点。
当我们在 GUI 中移动、复制、删除文件时,我们正在使用文件的硬链接而不是物理内存。当我们删除文件时,我们正在删除文件的硬链接。 我们不会清除物理内存。如果删除所有文件的硬链接,则将无法访问存储的数据,尽管它可能仍然存在于内存中
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.
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.
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
硬链接是unix的,就像它是在unix和linux中使用的旧链接一样,但符号链接在linux中是新的。
硬链接 inode 与原始文件 inode 相同。 但符号链接索引节点与原始文件索引节点不同。
硬链接文件大小(以字节为单位)与原始文件大小(以字节为单位)相同。 但符号链接文件大小(以字节为单位)与原始文件大小(以字节为单位)不同。 符号链接文件大小小于原始文件大小。
硬链接是原始文件的镜像副本。 符号链接或软链接就像 Windows 中的快捷方式。
如果删除原始文件,硬链接将保留其文件,您可以看到硬链接文件内容。 在符号链接中,如果删除原始文件,其符号链接将被破坏,并且符号链接仍然保留,但无法显示符号链接内容。
符号链接是新的,它有很多功能,但硬链接是旧的,这就是为什么它的功能较少。
让我们使用终端创建一些硬链接和符号链接:
echo“为什么这么严重”> file.txt
硬链接:
ln file.txt file_hard
符号链接:
ln -s file.txt file_sym
让我们看看 inode 的内容:
LS-李
hard link is unix like it is old one use in unix and then linux but symbolick link is new in linux.
hard link inode is same as original file inode. But symbolik link inode is different from original file inode.
Hard link file size in byte is same as original file size in byte. But symbolik link file size in byte is not as the original file size in byte. symbolick link file size is smaller then original file size.
Hard link is a mirror copy of original file . symbolick link or softlink is like shortcut in windows.
If you delete original file, hard link will remain its file and you can see hard link file content . In symbolick link, if you delete original file its symbolic link will broken and symbolick link still remain but it can not show symbolick link content.
Symbolick link is new and it is have many feature but hard link is old one thats why it is have less feature.
lets make some hard and symbolick link using terminal :
echo "why so serious" > file.txt
hard link:
ln file.txt file_hard
symbolick link:
ln -s file.txt file_sym
lets see there content with inode :
ls -li
我刚刚找到了一种简单的方法来理解常见场景(软件安装)中的硬链接。
有一天,我下载了一个软件到“下载”文件夹中进行安装。 在我执行 sudo make install 后,一些可执行文件被 cped 到本地 bin 文件夹。 在这里,cp 创建硬链接。 我对这个软件很满意,但很快意识到从长远来看,下载并不是一个好地方。 所以我将软件文件夹移动到源目录。 好吧,我仍然可以像以前一样运行该软件,而不必担心任何目标链接问题,就像在 Windows 中一样。 这意味着硬链接直接找到 inode 和周围的其他文件。
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 cped 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 mved 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.