Linux中的软链接和硬链接

时间:2020-03-21 11:47:14  来源:igfitidea点击:

本教程主要介绍如何在Linux系统中创建和管理指向文件的软链接和硬链接。
文件名存储在目录中的磁盘上。
仅文件名存储在目录中;实际的磁盘空间在其他地方。
因此,这显然意味着被命名的事物的名称和实际存储位于单独的位置。
文件名与存储空间分开保存在一个目录中,我的意思是硬盘对于事物进行命名是由于多个名称和多个目录中的名称;所有名称都指向相同的存储空间。
存储在linux机器磁盘中文件或者目录中的实际数据由数据构造函数“ Inodes”管理。
因此,众所周知,用户可以将一个文件与多个文件链接在一起,因此变得很困难当一个文件与许多文件链接在一起时,它可以查找原始文件。
因此,其中我们将使用readlink实用程序来查找所有链接的文件。

注意:软链接也称为符号链接。

Linux/Unix文件包含两个部分:

1.数据部分

2.文件名部分

数据部分与Inode关联。

Inode带有数据位置的地图。

文件名部分包含名称和关联的索引节点号。

硬链接说明:

当多个文件名可以引用相同的索引节点号时;这些文件被称为“硬链接”在一起。

为了详细解释和理解硬链接,我们创建了两个文件。

file1.txt和file2.txt,然后我们将学习如何链接,管理它们并在链接后查看两个文件的统计信息。

我们使用ln命令行实用程序创建硬链接。

[root@theitroad test]# cat > file1.txt
hii this is file1.txt ready for hard linking test.
tested by slashroot.in

[root@theitroad test]# ls -lh file1.txt
-rw-r--r-- 1 root root 74 Nov 18 03:12 file1.txt

[root@theitroad test]# ls -lih file1.txt
11010444 -rw-r--r-- 1 root root 74 Nov 18 03:12 file1.txt

所以现在我们可以清楚地看到

  • file1.txt的内容,
  • file1.txt的大小,为74个字节
  • file1.txt的权限为644
  • File1.txt的索引节点号为“ 11010444”

现在如何将file1.txt硬链接到file2.txt?

[root@theitroad test]# ln file1.txt file2.txt

因此上述命令用于将一个文件硬链接到另一个文件。

其中我将file1.txt硬链接到file2.txt。

现在,让我们看看两个硬链接文件之间的区别和相似之处。
例如:file1.txt和file2.txt。

[root@theitroad test]# ls -lih
total 8.0K
11010444 -rw-r--r-- 2 root root 74 Nov 18 03:12 file1.txt
11010444 -rw-r--r-- 2 root root 74 Nov 18 03:12 file2.txt

[root@theitroad test]# cat file2.txt
hii this is file1.txt ready for hard linking test.
tested by theitroad.com
[root@theitroad test]#

[root@theitroad test]# stat file1.txt
  File: `file1.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 03:26:48.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:20:04.000000000 +0530

[root@theitroad test]# stat file2.txt
  File: `file2.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 03:26:48.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:20:04.000000000 +0530

从上面的输出中,我们可以清楚地得出结论,对于硬链接:

  • 索引节点号不变,例如:相同的索引节点号。
  • 两个文件具有相同的权限
  • 相同的数据。
  • 相同大小
  • 相同的创建时间。
  • 唯一的名字是不同的。

注意:硬链接无法链接目录和不同文件系统上的文件。

软链接说明:

现在要创建软链接,我们再次需要两个文件以进行测试。
再次获取file1.txt,这次将其与file3.txt软链接。

如何创建软链接?

我们使用带有选项“ -s”的ln命令行实用程序来创建软链接。

软链接使用文件名或者目录。

[root@theitroad test]# ln -s file1.txt file3.txt

[root@theitroad test]# ls -lih
total 4.0K
11010444 -rw-r--r-- 1 root root 74 Nov 18 03:12 file1.txt
11010445 lrwxrwxrwx 1 root root  9 Nov 18 03:36 file3.txt -> file1.txt

[root@theitroad test]# cat file3.txt
hii this is file1.txt ready for hard linking test.
tested by theitroad.com
[root@theitroad test]#

[root@theitroad test]# stat file1.txt file3.txt
  File: `file1.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 03:38:42.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:37:14.000000000 +0530
  File: `file3.txt' -> `file1.txt'
  Size: 9               Blocks: 0          IO Block: 4096   symbolic link
Device: 803h/2051d      Inode: 11010445    Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 03:49:16.000000000 +0530
Modify: 2013-02-18 03:36:54.000000000 +0530
Change: 2013-02-18 03:36:54.000000000 +0530
[root@theitroad test]#

[root@theitroad test]# file file1.txt
file1.txt: ASCII text
[root@theitroad test]# file file3.txt
file3.txt: symbolic link to `file1.txt'
[root@theitroad test]#

从上面的输出中,我们可以清楚地看到:

  • 这两个文件具有不同的Inode编号。
  • 不同的权限。
  • 不同的大小。
  • 相同的内容。
  • 别的名字

如何创建目录链接?

首先创建两个目录dir1和dir2,并使用带有选项-s的ln命令将它们链接。

为了进行测试,目的是在dir1内创建四个文件,以查看它是否与dir2链接。

[root@theitroad test]# mkdir dir1
[root@theitroad test]# mkdir dir2

[root@theitroad test]# ln -s dir1 dir2

上面的命令用于链接目录。

[root@theitroad test]# tree
.
|-- dir1
|   |-- a
|   |-- b
|   |-- c
|   `-- d
`-- dir2
    `-- dir1 -> dir1

'2个目录,5个文件'

[root@theitroad test]# stat dir1 dir2
  File: `dir1'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 11010446    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 04:23:34.000000000 +0530
Modify: 2013-02-18 04:16:49.000000000 +0530
Change: 2013-02-18 04:16:49.000000000 +0530
  File: `dir2'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 11010447    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2013-02-18 04:23:34.000000000 +0530
Modify: 2013-02-18 04:16:03.000000000 +0530
Change: 2013-02-18 04:16:03.000000000 +0530

[root@theitroad test]# ls -li dir1
total 0
11010444 -rw-r--r-- 1 root root 0 Nov 18 04:16 a
11010445 -rw-r--r-- 1 root root 0 Nov 18 04:16 b
11010449 -rw-r--r-- 1 root root 0 Nov 18 04:16 c
11010450 -rw-r--r-- 1 root root 0 Nov 18 04:16 d

[root@theitroad test]# ls -li dir2
total 0
11010448 lrwxrwxrwx 1 root root 4 Nov 18 04:16 dir1 -> dir1

注意:将目录软链接到另一个目录可以通过多种方式进行。
但是,软链接目录的正确方法是在尚未创建另一个目录时将目录链接到另一个目录。

如何在Linux中使用readlink命令?

[root@theitroad ~]# readlink /etc/grub.conf
../boot/grub/grub.conf
 

[root@theitroad ~]# readlink -e /etc/grub.conf
/boot/grub/grub.conf
 

[root@theitroad ~]# readlink -f /etc/grub.conf
/boot/grub/grub.conf

它向我们展示了链接文件的最简单形式。

注意:“-e”和“ -f”选项激活规范化模式