hard link vs soft link

Inodes are associated with precisely one directory entry at a time. However, with hard links it is possible to associate multiple directory entries with a single inode. To create a hard link use ln command as follows:
# ln /root/file1 /root/file2
# ls -l

Above commands create a link to file1. Symbolic links refer to:

A symbolic path indicating the abstract location of another file.

Hard links refer to:

The specific location of physical data.

Hard link vs. Soft link in Linux or UNIX

  • Hard links cannot link directories.
  • Cannot cross file system boundaries.

Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:

  • To create links between directories.
  • Can cross file system boundaries.

These links behave differently when the source of the link is moved or removed.

  • Symbolic links are not updated.
  • Hard links always refer to the source, even if moved or removed.

How do I create symbolic link?

You can create symbolic link with ln command:
$ ln -s /path/to/file1.txt /path/to/file2.txt
$ ls -ali

Above command will create a symbolic link to file1.txt.

Task: Symbolic link creation and deletion

Let us create a directory called foo, enter:
$ mkdir foo
$ cd foo

Copy /etc/resolv.conf file, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:19 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali

Sample output:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:24 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048602 lrwxrwxrwx   1 vivek vivek     11 2008-12-09 20:24 alink.conf -> resolv.conf
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali

If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.

在Linux系统中,根目录(通常表示为"/")下创建文件"linux"是一个常规操作,可以使用文本编辑器如`nano`、`vim` 或 `echo` 命令直接创建: ```bash # 创建普通文件 touch /linux # 进入/tmp目录并创建硬链接 cd /tmp ln -s /linux linux.soft ``` 软链接(Soft Link)和硬链接(Hard Link)都是指向原始文件的指针,但它们之间有以下区别: 1. **内容**: - 软链接就像一个快捷方式,它保存的是目标文件路径,而不是文件内容本身。 - 硬链接则是文件系统的实际引用,它保存的是文件数据块的直接地址,因此两个硬链接共享同一份文件数据。 2. **权限和设备**: - 如果是跨分区或设备的软链接,系统会报告错误,因为软链接依赖于路径解析。 - 硬链接不受此限制,可以在任何位置对另一个分区或设备上存在的文件创建硬链接。 3. **持久性**: - 当原文件被删除时,只有最后一个指向它的硬链接仍然有效,其他硬链接会失效,除非该硬链接也在同一次系统重启前删除。 - 而软链接不受此影响,当源文件被删除时,软链接将无法再打开,成为无效链接。 4. **系统资源**: - 创建软链接几乎无额外开销,因为它只包含链接路径。 - 硬链接需要额外的空间来存储链接计数,因此当有很多硬链接时可能会占用更多空间。 5. **显示**: - 使用`ls -i` 命令可以看到硬链接共享相同的inode号,而软链接会有单独的inode号。 综上所述,硬链接适合大量并发访问,而软链接则用于提供便利的指向,如果不需要共享数据内容,或者涉及到跨越分区的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值