ln创建链接请使用绝对路径!!!
链接是个好东西,可以把原文件link到一个链接上,通过链接可以读取原文件,也可以修改原文件。链接可以同步原文件的改动,即链接和原文件内容始终相同。以上的功能不区分软链接还是硬链接,是链接的基本属性。
硬链接
硬链接创建后,像是copy了原文件,地位和原文件相同,原文件被删除链接文件依然有内容,不同于copy的地方是硬链接是一个指向原文件在磁盘中地址的索引(即硬链接占用的磁盘空间是作为链接的空间,不是文件的空间)
ln /absolute/path/to/target /absolute/path/to/link
创建完链接,可以使用 ls -l
命令查看,如果正确创建(使用 vim
打开链接,如果是空白则创建失败),会看到链接文件大小和原文件大小相同(ls查看的是数据在磁盘的大小,原文件和硬链接都是一个指向磁盘数据的索引)
软链接
软链接像是windows桌面的快捷方式,作用是指向原文件,原文件被删除则链接文件失效。(与硬链接的区别是,软链接不指向磁盘中的数据,而是指向原文件)
Linux 中 file 是一个指向磁盘数据的索引,
rm file
实际上只是删除了索引,并没有真正清空磁盘中的数据,所以才会有使用xx软件恢复误删数据的可能.
ln -s /absolute/path/to/target /absolute/path/to/link
软链接的大小很小,同样可以用上述方法查看
请使用绝对路径创建链接
如果 ln
命令创建链接失败了,那么请使用绝对路径吧🙃!(如果是权限问题,加上 sudo
)
(附)pwn.college关于硬链接与软链接的解释
Links come in two flavors: hard and soft (also known as symbolic) links. We’ll differentiate the two with an analogy:
- A hard link is when you address your appartment using multiple addresses that all lead directly to the same place (e.g., Apt 2 vs Unit 2).
- A soft link is when you move appartments and have the postal service automatically forward your mail from your old place to your new place.
In a filesystem, a file is, conceptually, an address at which the contents of that file live. A hard link is an alternate address that indexes that data — accesses to the hard link and accesses to the original file are completely identical, in that they immediate yield the necessary data. A soft/symbolic link, instead, contains the original file name. When you access the symbolic link, Linux will realize that it is a symbolic link, read the original file name, and then (typically) automatically access that file. In most cases, both situations result in accessing the original data, but the mechanisms are different.
Hard links sound simpler to most people (case in point, I explained it in one sentence above, versus two for soft links), but they have various downsides and implementation gotchas that make soft/symbolic links, by far, the more popular alternative.