bash 使用“ln”命令的 Linux 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17536587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux links with "ln" command
提问by Jcao02
I'm writing a shell script in bash where I'm making some links between files, but I'm not sure what kind of link to use (physical or symbolic). Doing some research, I've noticed that it's more common to use symbolic links instead of physical ones. My question is why to use symbolic links when they require an indirection (it creates an extra I-node to safe the information about the real I-node of the file) instead of using hard links that point directly to the file?
我正在用 bash 编写一个 shell 脚本,在其中我在文件之间建立一些链接,但我不确定要使用哪种链接(物理或符号)。做了一些研究,我注意到使用符号链接而不是物理链接更常见。我的问题是为什么在需要间接引用时使用符号链接(它创建一个额外的 I 节点来保护有关文件的真实 I 节点的信息)而不是使用直接指向文件的硬链接?
In other words:
换句话说:
Why
为什么
ln -s ...
instead of
代替
ln -P ...
回答by Marc B
The main reason for symlinks is that a 'soft' symlink can cross filesystem boundaries. The file representing the symlink will contain a string that is the actual path of the file being pointed at. As long as the end-user representations of that path remain the same, the symlink will work. If you move the file at the end of the symlink, the symlink will now be stale (aka "dangling"), because the resource it pointed at no longer exists.
符号链接的主要原因是“软”符号链接可以跨越文件系统边界。表示符号链接的文件将包含一个字符串,该字符串是所指向文件的实际路径。只要该路径的最终用户表示保持不变,符号链接就会起作用。如果您在符号链接的末尾移动文件,符号链接现在将失效(又名“悬空”),因为它指向的资源不再存在。
A hard (aka physical) symlink works at the inode layer. Since inodes are only unique within a single file system, you cannot hardlink ACROSS file systems. You could quite easily run into a duplicate inode situation if this were allowed. The benefit is that no matter where you move the target of a hardlink, the links pointing at the resource will "follow", because they're pointing at the inode itself, and don't care what the actual path/resource name is.
硬(又名物理)符号链接在 inode 层工作。由于 inode 仅在单个文件系统中是唯一的,因此您不能硬链接 ACROSS 文件系统。如果允许,您很容易遇到重复的 inode 情况。好处是无论您将硬链接的目标移动到何处,指向资源的链接都会“跟随”,因为它们指向 inode 本身,而不关心实际路径/资源名称是什么。
回答by mob
Off the top of my head:
在我的头顶:
symbol links work across filesystems. If you don't want to keep track of what filesystem the source file and destination link are on, or if you sometimes move files across filesystems, it is less of a hassle to use symbolic links.
$@&#$&
emacsbackup files. When you edit, sayfile.txtand make a change to it in emacs, emacs renames the original file tofile.txt~and saves your changes under the original file name. If there was a hard link tofile.txt, it is now linked tofile.txt~, which is probably not what you want. A symbolic link tofile.txtwill still point to the updated inode.
符号链接跨文件系统工作。如果您不想跟踪源文件和目标链接所在的文件系统,或者有时跨文件系统移动文件,那么使用符号链接就不那么麻烦了。
$ @ $&
emacs备份文件。当您file.txt在 emacs 中编辑、说并对其进行更改时,emacs 会将原始文件重命名为file.txt~并将更改保存在原始文件名下。如果存在指向 的硬链接file.txt,则现在链接到file.txt~,这可能不是您想要的。符号链接file.txt仍将指向更新的 inode。
回答by Zak
A hardlink can only work on the same filesystem, renames the inode. A file can only be deleted when the last link to its inode is gone. Hardlinks usually are for files instead of directories.
硬链接只能在同一个文件系统上工作,重命名 inode。文件只能在指向其 inode 的最后一个链接消失后才能删除。硬链接通常用于文件而不是目录。
A symlink is an actual file containing a path to another file. Symlinks can work across file systems as well. They can point to different file types as well. A symlink can also point to either files or directories.
符号链接是包含另一个文件路径的实际文件。符号链接也可以跨文件系统工作。它们也可以指向不同的文件类型。符号链接也可以指向文件或目录。
回答by cHao
Hard links don't make sense across filesystems, since they're basically pointers to inodes on the local FS. Symlinks don't care; since they're just names, they can live anywhere.
If you look at the directory listing and see a symlink, you know it's to a particular other location. Hard links, on the other hand, offer no such clue. You might not know you were playing around in some important file til you
statboth its names.
硬链接跨文件系统没有意义,因为它们基本上是指向本地 FS 上的 inode 的指针。符号链接不在乎;因为它们只是名字,所以它们可以住在任何地方。
如果您查看目录列表并看到符号链接,您就知道它指向其他特定位置。另一方面,硬链接没有提供这样的线索。你可能不知道你在玩一些重要的文件,直到你知道
stat它的名字。

