最近我在一次工作面试中被问到这个问题。我诚实地说,我知道符号链接的行为和如何创建一个,但不了解硬链接的使用,以及它与符号链接的区别。
当前回答
硬链接是Unix,它在Unix和Linux中都是旧的,但符号链接在Linux中是新的。
硬链接inode与原始文件inode相同。但是symbolik链接索引节点不同于原始文件索引节点。
硬链接文件的字节大小与原始文件的字节大小相同。但是符号链接文件的字节大小不像原始文件的字节大小。符号链接文件大小小于原始文件大小。
硬链接是原始文件的镜像副本。符号链接或软链接就像窗口中的快捷方式。
如果您删除原始文件,硬链接将保留其文件,您可以看到硬链接文件的内容。在符号链接中,如果删除原始文件,其符号链接将断开,符号链接仍然保留,但不能显示符号链接内容。
符号链接是新的,它有很多特点,但硬链接是旧的,这就是为什么它有较少的特点。
让我们用终端做一些硬的和象征性的链接: Echo“为什么这么严重”> file.txt
硬链接: Ln file.txt file_hard
symbolick链接: Ln -s file.txt file_sym
让我们看看inode的内容: ls李津
其他回答
当原始文件被移动时,硬链接非常有用。例如,将文件从/bin移动到/usr/bin或/usr/local/bin。到/bin中文件的任何符号链接都将被破坏,但是硬链接(直接到文件的inode的链接)不会关心。
硬链接可能占用更少的磁盘空间,因为它们只占用一个目录条目,而符号链接需要自己的inode来存储它所指向的名称。
Hard links also take less time to resolve - symlinks can point to other symlinks that are in symlinked directories. And some of these could be on NFS or other high-latency file systems, and so could result in network traffic to resolve. Hard links, being always on the same file system, are always resolved in a single look-up, and never involve network latency (if it's a hardlink on an NFS filesystem, the NFS server would do the resolution, and it would be invisible to the client system). Sometimes this is important. Not for me, but I can imagine high-performance systems where this might be important.
I also think things like mmap(2) and even open(2) use the same functionality as hardlinks to keep a file's inode active so that even if the file gets unlink(2)ed, the inode remains to allow the process continued access, and only once the process closes it does the file really go away. This allows for much safer temporary files (if you can get the open and unlink to happen atomically, which there may be a POSIX API for that I'm not remembering, then you really have a safe temporary file) where you can read/write your data without anyone being able to access it. Well, that was true before /proc gave everyone the ability to look at your file descriptors, but that's another story.
说到这里,恢复一个在进程a中打开,但在文件系统中未链接的文件需要使用硬链接来重新创建inode链接,这样当打开该文件的进程关闭或离开时,该文件不会消失。
从MSDN,
符号链接
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
绝对符号链接的一个例子
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对它的定义如下:
连接(也称为软链接)与硬链接的不同之处在于,它引用的存储对象是单独的目录,并且连接可以链接位于同一计算机上不同本地卷上的目录。否则,连接与硬链接的操作相同。
硬链接部分和结节部分的粗体部分显示了两者的基本区别。
命令在窗口中创建一个连接,导航到要创建链接的位置,然后输入:
mklink /J link_name target_path
软链接:
软的或象征性的更像是原始文件....的捷径如果您删除原始的快捷方式失败,如果您只删除快捷方式,原始的不会发生任何变化。
软链接语法:ln -s Pathof_Target_file链接
输出:link -> ./Target_file
证明:readlink链接 同样,在ls -l链接输出中,您将看到lrwxrwxrwx中的第一个字母为l,这表明该文件是一个软链接。
删除链路:unlink链路
注意:如果你愿意,你的软链接即使从当前目录移动到其他地方也可以工作。在创建软链接时,请确保您给出的是绝对路径而不是相对路径。即(从/root/user/Target_file开始,而不是。/Target_file)
硬链接:
硬链接更多的是镜像副本或同一文件的多条路径。对file1做一些操作,它就会出现在文件2中。 删除一个仍然可以保留另一个。
inode(或文件)只有在所有(硬)链接或(同一文件)inode的所有路径都已删除时才会被删除。
一旦创建了硬链接,该链接就具有原始文件的inode。删除重命名或移动原始文件不会影响硬链接,因为它链接到底层inode。对inode上数据的任何更改都反映在引用该inode的所有文件中。
硬链接语法:ln Target_file链接
输出:将创建一个名称为link的文件,其索引节点号与Targetfile相同。
证明:ls -i link Target_file(检查它们的索引节点)
删除链接:rm -f link(像删除普通文件一样删除链接)
注意:符号链接可以跨文件系统,因为它们只是另一个文件的名称。而硬链接只在同一个文件系统中有效。
符号链接有一些硬链接所没有的特性:
Hard link point to the file content. while Soft link points to the file name. while size of hard link is the size of the content while soft link is having the file name size. Hard links share the same inode. Soft links do not. Hard links can't cross file systems. Soft links do. you know immediately where a symbolic link points to while with hard links, you need to explore the whole file system to find files sharing the same inode. # find / -inum 517333 /home/bobbin/sync.sh /root/synchro hard-links cannot point to directories.
硬链接有两个限制:
目录不支持硬链接。Linux不允许这样维护目录的非循环树结构。 不能跨文件系统创建硬链接。这两个文件必须在相同的文件系统上,因为不同的文件系统有不同的独立的inode表(两个文件在不同的文件系统上,但是具有相同的inode号将是不同的)。
另外:
硬链接的读取性能优于符号链接(微性能) 符号链接可以被复制,版本控制,等等。换句话说,它们是一个实际的文件。另一方面,硬链接的级别略低,您会发现,与符号链接相比,提供将硬链接作为硬链接而不是普通文件处理的工具较少
我对使用的两点看法:
软链接可以用来缩短长路径名,例如:
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上的文件
推荐文章
- 如何从命令行通过mysql运行一个查询?
- 在创建守护进程时执行双fork的原因是什么?
- 对以制表符分隔的文件进行排序
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- 如何在C程序中获取当前目录?
- 如何在Bash中逐行合并两个文件
- 如何从远程SSH会话发送数据到本地剪贴板
- 我如何得到bash完成工作与别名?
- SSH端口转发~/。ssh /配置文件?
- 如何将多个文件的内容追加到一个文件
- 如何进入每个目录并执行命令?