符号链接和硬链接有什么区别?

发布于 2024-07-07 04:19:21 字数 75 浏览 5 评论 0原文

最近我在面试时被问到这个问题。 我很诚实地说我知道符号链接的行为方式以及如何创建符号链接,但不了解硬链接的使用以及它与符号链接的区别。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

默嘫て 2024-07-14 04:19:21

在文件系统下面,文件由索引节点表示。 (或者是多个 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.

十六岁半 2024-07-14 04:19:21

俗话说,一图胜千言。 以下是我如何将其可视化:

在此处输入图像描述

以下是我们如何获取该图片:

  1. 创建名称文件系统中的 myfile.txt 指向一个新的 inode(其中包含文件的元数据并指向包含其内容的数据块,即文本“Hello, World!”):

    $ echo '你好,世界!'   >   我的文件.txt 
      
  2. 创建到文件 myfile.txt 的硬链接 my-hard-link,这意味着“创建一个文件应该指向 myfile.txt 指向的同一个 inode":

    $ ln myfile.txt my-hard-link 
      
  3. 创建指向文件 myfile.txt 的软链接 my-soft-link,其中意思是“创建一个应该指向文件myfile.txt的文件”:

    $ ln -s myfile.txt my-soft-link 
      

看看如果删除(或移动)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:

enter image description here

Here is how we get to that picture:

  1. 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!":

    $ echo 'Hello, World!' > myfile.txt
    
  2. Create a hard link my-hard-link to the file myfile.txt, which means "create a file that should point to the same inode that myfile.txt points to":

    $ ln myfile.txt my-hard-link
    
  3. Create a soft link my-soft-link to the file myfile.txt, which means "create a file that should point to the file 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.

轮廓§ 2024-07-14 04:19:21

一些可能有帮助的例子。

创建两个包含数据的文件:

$ printf Cat > foo
$ printf Dog > bar

创建硬链接和软链接(也称为符号链接):

$ ln foo foo-hard
$ ln -s bar bar-soft

通过增加大小以长格式列出目录内容:

ls -lrS
lrwxr-xr-x   1 user  staff        3  3 Apr 15:25 bar-soft -> bar
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo-hard
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo
-rw-r--r--   1 user  staff        4  3 Apr 15:25 bar

这告诉我们

  • 第一列:软链接和硬链接的文件模式不同< /p>

    • 软链接:lrwxr-xr-x
      • 文件类型:l = 符号链接
      • 所有者权限:rwx = 可读、可写、可执行
      • 组权限:rx = 可读、不可写、可执行
      • 其他权限:rx = 可读、不可写、可执行
    • 硬链接:-rw-r--r--
      • 文件类型:- = 常规文件
      • 所有者权限:rw- = 可读、可写、不可执行
      • 组权限:r-- = 可读、不可写、不可执行
      • 其他权限:r-- = 可读、不可写、不可执行
  • 第二列:硬链接文件的链接数较高

  • 第五列:软链接的大小较小,因为它是引用而不是副本

  • 最后一列:符号链接通过 ->

更改 foo 的文件名不会影响 foo-hard:

$ mv foo foo-new
$ cat foo-hard
Cat

更改 foo 的内容反映在 foo-hard:

$ printf Dog >> foo
$ cat foo-hard
CatDog

硬链接 中foo-hard 指向文件的 inode、内容。

对于像 bar-soft 这样的软链接来说,情况并非如此:

$ mv bar bar-new
$ ls bar-soft
bar-soft
$ cat bar-soft  
cat: bar-soft: No such file or directory

无法找到文件的内容,因为软链接指向已更改的名称,而不是内容。

同样,如果 foo 被删除,foo-hard 仍然保留内容; 如果 bar 被删除,bar-soft 只是一个指向不存在文件的链接。

Some examples that might help.

Create two files with data in them:

$ printf Cat > foo
$ printf Dog > bar

Create a hard and soft (aka symbolic) link:

$ ln foo foo-hard
$ ln -s bar bar-soft

List directory contents in long format by increasing size:

ls -lrS
lrwxr-xr-x   1 user  staff        3  3 Apr 15:25 bar-soft -> bar
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo-hard
-rw-r--r--   2 user  staff        4  3 Apr 15:25 foo
-rw-r--r--   1 user  staff        4  3 Apr 15:25 bar

This tell us that

  • 1st column: the file mode for the soft and hard links differ

    • soft link: lrwxr-xr-x
      • filetype: l = symbolic link
      • owner permissions: rwx = readable, writable, executable
      • group permissions: r-x = readable, not writable, executable
      • other permissions: r-x = readable, not writable, executable
    • hard link: -rw-r--r--
      • filetype: - = regular file
      • owner permissions: rw- = readable, writable, not executable
      • group permissions: r-- = readable, not writable, not executable
      • other permissions: r-- = readable, not writable, not executable
  • 2nd 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:

$ mv foo foo-new
$ cat foo-hard
Cat

Changing the contents of foo is reflected in foo-hard:

$ printf Dog >> foo
$ cat foo-hard
CatDog

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:

$ mv bar bar-new
$ ls bar-soft
bar-soft
$ cat bar-soft  
cat: bar-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 foo is deleted, foo-hard still holds the contents; if bar is deleted, bar-soft is just a link to a non-existing file.

亚希 2024-07-14 04:19:21

当原始文件被移动时,硬链接非常有用。 例如,将文件从 /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.

小苏打饼 2024-07-14 04:19:21

了解硬链接和符号链接之间区别的简单方法是通过一个简单的示例。 文件的硬链接将指向文件存储的位置,或该文件的索引节点。 符号链接将指向实际文件本身。

因此,如果我们有一个名为“a”的文件并创建一个硬链接“b”和一个符号链接“c”,它们都引用文件“a”:

echo "111" > a
ln a b
ln -s a c

“a”、“b”和“c”的输出将be :

cat a --> 111
cat b --> 111
cat c --> 111

现在让我们删除文件“a”,看看“a”、“b”和“c”的输出会发生什么:

rm a
cat a --> No such file or directory
cat b --> 111
cat c --> No such file or directory

那么发生了什么?

因为文件“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" :

echo "111" > a
ln a b
ln -s a c

The output of "a", "b", and "c" will be :

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":

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.

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.

蝶…霜飞 2024-07-14 04:19:21

软链接

软链接或符号链接更多的是原始文件的快捷方式......如果删除原始文件,快捷方式将失败,而如果您只删除快捷方式原版没有任何变化。

软链接语法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

    <块引用>

    <代码>/home/bobbin/sync.sh 
      /根/同步 
      
  • 硬链接不能指向目录。

硬链接有两个限制:

  • 目录不能被硬链接。 Linux 不允许这样维护目录的非循环树结构。
  • 无法跨文件系统创建硬链接。 这两个文件必须位于相同的文件系统上,因为不同的文件系统具有不同的独立 inode 表(不同文件系统上的两个文件,但具有相同的 inode 编号会不同)。

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 in lrwxrwxrwx 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:

  • 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.

The hard links have two limitations:

  • The directories cannot be hard linked. Linux does not permit this to maintain the acyclic tree structure of directories.
  • A hard link cannot be created across filesystems. Both the files must be on the same filesystems, because different filesystems have different independent inode tables (two files on different filesystems, but with same inode number will be different).
自此以后,行同陌路 2024-07-14 04:19:21

符号链接链接到路径名。 它可以位于系统文件树中的任何位置,甚至在创建链接时不必存在。 目标路径可以是相对路径或绝对路径。

硬链接是指向 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.

萌化 2024-07-14 04:19:21

我建议您访问维基百科:

几点:

  • 与硬链接不同,符号链接可以跨文件系统(大多数时候)。
  • 符号链接可以指向目录。
  • 硬链接指向一个文件,使您能够使用多个名称引用同一文件。
  • 只要至少有一个链接,数据仍然可用。

I would point you to Wikipedia:

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.
单挑你×的.吻 2024-07-14 04:19:21

硬链接 vs软链接

硬链接与软链接可以通过此图片轻松解释。

Hard link vs Soft link

Hard link Vs Soft link can be easily explained by this image.

[旋木] 2024-07-14 04:19:21

进行增量备份时,硬链接非常有用。 例如,请参阅 rsnapshot。 这个想法是使用硬链接进行复制:

  • 将备份编号 n 复制到 n + 1
  • 将备份 n - 1 复制到 n
  • ...
  • 将备份 0 复制到备份 1
  • 使用任何更改的文件更新备份 0。

除了您所做的任何更改之外,新备份不会占用任何额外空间,因为所有增量备份都将指向未更改文件的同一组索引节点。

Hard links are very useful when doing incremental backups. See rsnapshot, for example. The idea is to do copy using hard links:

  • copy backup number n to n + 1
  • copy backup n - 1 to n
  • ...
  • copy backup 0 to backup 1
  • update backup 0 with any changed files.

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.

倾城泪 2024-07-14 04:19:21

来自 MSDN

< a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365680(v=vs.85).aspx" rel="noreferrer">符号链接

符号链接是指向另一个文件系统对象的文件系统对象。 所指向的对象称为目标。

符号链接对用户是透明的; 链接显示正常
文件或目录,并且可以由用户或应用程序执行
以完全相同的方式。

符号链接旨在帮助迁移和应用
与 UNIX 操作系统的兼容性。 微软已经实施了
它的符号链接的功能就像 UNIX 链接一样。

符号链接可以是绝对链接,也可以是相对链接。 绝对
links 是指定路径名各部分的链接; 相对的
链接是相对于相对链接说明符所在的位置确定的
指定路径

绝对符号链接示例

X: "C:\alpha\beta\absLink\gamma\file"
Link: "absLink" maps to "\\machineB\share"
Modified Path: "\\machineB\share\gamma\file"

相对符号链接示例

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"

硬链接

硬链接是文件的文件系统表示形式,通过它
多个路径引用同一卷中的单个文件。

要在 Windows 中创建硬链接,请导航到要创建链接的位置并输入以下命令:

mklink /H Link_name target_path

请注意,您可以按任意顺序删除硬链接,无论它们的创建顺序如何。 时,无法创建硬链接

  • 此外,当引用位于不同的本地驱动器
  • 引用包括网络驱动器 。 换句话说,引用之一是
  • 要创建的网络驱动器硬链接与目标

连接

NTFS 支持另一种称为连接的链接类型。 MSDN对其的定义如下:

联结(也称为软链接)与硬链接的不同之处在于,它引用的存储对象是单独的目录,并且联结可以链接位于同一计算机上不同本地卷的目录。 否则,连接的操作与硬链接相同。

硬链接部分和连接部分中的粗体部分显示了两者之间的基本区别。

在 Windows 中创建连接的命令,导航到要创建链接的位置,然后输入:

mklink /J link_name target_path

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.

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:

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:

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:

mklink /J link_name target_path
风透绣罗衣 2024-07-14 04:19:21

这个答案适用于 Web 开发人员

This answers is for Web Developers ????:

Hard Link: like pointing different domain names to the same host

* abc.com and def.com -> points to the IP 1.2.3.4
* abc.com and def.com -> files in linux
* IP -> inode in linux
* By deleting the domain abc.com users still access your website through def.com or vice versa
* Mutation through abc.com will affect def.com and vice verca

Sym Link : redirecting one domain to another domain

* accessing abc.com will redirect to def.com as you've accessed def.com directly
* removing the def.com domain will break the link abc.com
始终不够 2024-07-14 04:19:21

简单来说,硬链接:只是给一个文件添加新的名称,也就是说,一个文件可以同时有多个名称,所有名称都相同,没有谁优先,硬链接并不意味着复制所有内容 则

符号链接(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.

栖迟 2024-07-14 04:19:21

我对用法的两点意见:

链接可用于缩短长路径名,即:

ln -s /long/folder/name/on/long/path/file.txt /short/file.txt

/short/file.txt所做的更改将应用​​于原始文件。

链接可用于移动大文件:

$ ls -lh /myapp/dev/
total 10G
-rw-r--r-- 2 root root 10G May 22 12:09 application.bin

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.:

ln -s /long/folder/name/on/long/path/file.txt /short/file.txt

Changes made to /short/file.txt will be applied on the original file.

Hard links may be used to move around big files:

$ ls -lh /myapp/dev/
total 10G
-rw-r--r-- 2 root root 10G May 22 12:09 application.bin

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

泪眸﹌ 2024-07-14 04:19:21

另外:

  1. 硬链接的读取性能优于符号链接(微性能)
  2. 符号链接可以复制、版本控制等。 换句话说,它们是一个实际的文件。 另一方面,硬链接的级别稍低,您会发现与符号链接相比,提供将硬链接用作硬链接而不是普通文件的工具较少

Also:

  1. Read performance of hard links is better than symbolic links (micro-performance)
  2. 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
只是在用心讲痛 2024-07-14 04:19:21

您所认为的普通“文件”实际上是两个独立的东西:文件的数据和目录条目。 当您为文件创建硬链接时,您实际上创建了引用相同数据的第二个目录条目。 两个目录条目具有完全相同的功能; 每一个都可以用来打开文件来读取它。 所以你实际上并没有“一个文件加一个硬链接”,你有“带有两个目录条目的文件数据”。 您所认为的删除文件实际上会删除一个目录条目,当删除数据的最后一个目录条目时,数据本身也会被删除。 对于只有一个目录项的普通文件,删除该目录项也会一如既往地删除数据。 (打开文件时,操作系统会创建到该文件的临时链接,因此即使您删除所有目录条目,数据也会保留,但一旦关闭文件就会消失)。

例如,创建文件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.

尸血腥色 2024-07-14 04:19:21

目录项链接一个结构体:

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 */
         ...
    }

then您创建一个硬链接(可能是 /100),目录项可能像:

  struct dentry{
     ino_t ino; /* 15 */
     char  name[256]; /* 100 */
  }

inode 结构可能像:

   struct inode{ /* inode numebr 15 */
         link_t nlink; /* nlink = 2 */
         ...
    }

然后您创建一个到文件 1 的符号链接(可能是 /200),目录项可能像:

  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 */

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:

struct inode{
      link_t nlink; 
      ...
}

for example you creat a file /1, the directory entry maybe like:

struct dentry{
     ino_t ino; /* such as 15 */
     char  name[256]; /* "1" */
} 

the inode struct maybe like:

   struct inode{ /* inode number 15 */
         link_t nlink; /* nlink = 1 */
         ...
    }

then you create a hard link(may be /100), the directory entry maybe like:

  struct dentry{
     ino_t ino; /* 15 */
     char  name[256]; /* 100 */
  }

the inode struct maybe like:

   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:

  struct dentry{
        ino_t ino; /* such as 16 */
        char  name[256]; /* "200" */
  }

the inode struct maybe like:

   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 */
南…巷孤猫 2024-07-14 04:19:21

添加到上述所有答案,查找硬链接和软链接文件的区别可以理解如下:

我当前目录中有一个文件 f6 ,以及一个名为 t2< 的目录/代码>。

名为 f1./t2/f2 的文件是指向 f6 的符号链接。

名为f7./t2/f8的文件是f6的硬链接。

要查找软链接和硬链接,我们可以使用:

$ find -L . -samefile f6 

> ./f1
> ./f6
> ./f7
> ./t2/f2
> ./t2/f8

要仅查找硬链接,我们可以使用:

$ find . -xdev -samefile f6

> ./f6
> ./f7
> ./t2/f8

由于可以在同一文件系统上创建硬链接,因此我们可以在不使用 -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 named t2.

File named f1 and ./t2/f2 are symbolic links to f6.

File named f7 and ./t2/f8 are hard links of 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.

So searching the hardlink is somewhat faster then searching the softlinks(Please rectify if I am wrong or not clear).

三五鸿雁 2024-07-14 04:19:21

符号链接为文件提供另一个名称,其方式类似于硬链接。 但即使存在剩余的符号链接,也可以删除文件。

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.

轮廓§ 2024-07-14 04:19:21

我刚刚找到了一种简单的方法来理解常见场景(软件安装)中的硬链接。

有一天,我将一个软件下载到文件夹 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 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.

梦在深巷 2024-07-14 04:19:21

在这个答案中,当我说文件时,我的意思是内存中的位置

所有保存的数据都使用称为索引节点的数据结构存储在内存中每个索引节点都有一个索引节点号。索引节点号用于访问索引节点。所有硬链接一个文件可能有不同的名称,但共享相同的 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

青丝拂面 2024-07-14 04:19:21

硬链接是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

红尘作伴 2024-07-14 04:19:21

我刚刚找到了一种简单的方法来理解常见场景(软件安装)中的硬链接。

有一天,我下载了一个软件到“下载”文件夹中进行安装。 在我执行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文