Linux 文件、目录与链接及文件系统详解
1. 硬链接(Hard Links)
硬链接是指向文件索引节点(inode)的目录项。每个文件都有一个索引节点,其中包含有关该文件的信息,包括文件数据的位置。每个索引节点至少被一个目录项引用,有时会被多个目录项引用。
以下是创建硬链接的实验步骤:
1. 以
student
用户身份,将
~/testdir
设为当前工作目录(PWD),并删除该目录下的所有文件:
[student@studentvm1 testdir]$ cd ~/testdir ; rm -rf * ; ll
total 0
- 创建一个包含简单文本内容的文件,并列出目录内容:
[student@studentvm1 testdir]$ echo "Hello World" > file001 ; ll
total 4
-rw-rw---- 1 student student 12 Feb 12 11:59 file001
这里权限和所有者之间的数字
1
表示该文件的硬链接数量。因为只有一个目录项指向此文件,所以只有一个链接。可以使用
stat
命令验证:
[student@studentvm1 testdir]$ stat file001
File: file001
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 253,4 Inode: 2182 Links: 1
Access: (0660/-rw-rw----) Uid: ( 1000/ student) Gid: ( 1000/ student)
Access: 2023-02-12 11:59:41.942185794 -0500
Modify: 2023-02-12 11:59:41.942185794 -0500
Change: 2023-02-12 11:59:41.942185794 -0500
Birth: 2023-02-12 11:59:41.942185794 -0500
-
创建该文件的硬链接。
ln实用程序默认创建硬链接:
[student@studentvm1 testdir]$ ln file001 link1 ; ll
total 8
-rw-rw---- 2 student student 12 Feb 12 11:59 file001
-rw-rw---- 2 student student 12 Feb 12 11:59 link1
此时两个目录项的链接计数都变为
2
。显示两个文件的内容并对它们使用
stat
命令:
[student@studentvm1 testdir]$ cat file001 link1
Hello World
Hello World
[student@studentvm1 testdir]$ stat file001 link1
File: file001
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 253,4 Inode: 2182 Links: 2
Access: (0660/-rw-rw----) Uid: ( 1000/ student) Gid: ( 1000/ student)
Access: 2023-02-12 12:06:11.752916155 -0500
Modify: 2023-02-12 11:59:41.942185794 -0500
Change: 2023-02-12 12:04:34.457983455 -0500
Birth: 2023-02-12 11:59:41.942185794 -0500
File: link1
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 253,4 Inode: 2182 Links: 2
Access: (0660/-rw-rw----) Uid: ( 1000/ student) Gid: ( 1000/ student)
Access: 2023-02-12 12:06:11.752916155 -0500
Modify: 2023-02-12 11:59:41.942185794 -0500
Change: 2023-02-12 12:04:34.457983455 -0500
Birth: 2023-02-12 11:59:41.942185794 -0500
可以看到两个文件的所有元数据(包括索引节点号和链接数)都是相同的。
4. 在同一目录中创建另一个链接:
[student@studentvm1 testdir]$ ln link1 link2 ; ll
total 12
-rw-rw---- 3 student student 12 Feb 12 11:59 file001
-rw-rw---- 3 student student 12 Feb 12 11:59 link1
-rw-rw---- 3 student student 12 Feb 12 11:59 link2
可以对这三个文件使用
stat
命令来验证它们的元数据是否相同。
5. 在主目录中创建指向该索引节点的链接:
[student@studentvm1 testdir]$ ln link1 ~/link3 ; ll ~/link*
-rw-rw---- 4 student student 12 Feb 12 11:59 link3
现在该文件有四个硬链接。可以使用
ls -li
或
ll -i
命令查看索引节点号:
[student@studentvm1 testdir]$ ll -i
total 12
2182 -rw-rw---- 4 student student 12 Feb 12 11:59 file001
2182 -rw-rw---- 4 student student 12 Feb 12 11:59 link1
2182 -rw-rw---- 4 student student 12 Feb 12 11:59 link2
-
尝试从
/tmp创建硬链接:
[student@studentvm1 testdir]$ link file001 /tmp/link4
link: cannot create link '/tmp/link4' to 'file001': Invalid cross-device link
这个尝试失败了,因为
/tmp
和
/home
是不同的文件系统。硬链接仅限于单个文件系统内的文件,因为索引节点号仅在每个文件系统内是唯一的。
2. 查找具有多个硬链接的文件
find
命令可以查找具有多个硬链接的文件。以下是查找具有四个硬链接的文件的实验:
[root@studentvm1 ~]# find / -type f -links 4
/home/student/link3
/home/student/testdir/link2
/home/student/testdir/file001
/home/student/testdir/link1
/usr/sbin/fsck.ext2
/usr/sbin/mkfs.ext3
/usr/sbin/mke2fs
/usr/sbin/mkfs.ext4
/usr/sbin/e2fsck
/usr/sbin/fsck.ext3
/usr/sbin/mkfs.ext2
/usr/sbin/fsck.ext4
还可以查找
mkfs
文件的索引节点号:
[root@studentvm1 ~]# find / -type f -name mkfs*[0-9] -links 4 -exec ls -li {} \;
264063 -rwxr-xr-x. 4 root root 136976 Jul 20 2022 /usr/sbin/mkfs.ext2
264063 -rwxr-xr-x. 4 root root 136976 Jul 20 2022 /usr/sbin/mkfs.ext3
264063 -rwxr-xr-x. 4 root root 136976 Jul 20 2022 /usr/sbin/mkfs.ext4
这三个文件具有相同的索引节点号
264063
,说明它们是同一个文件的多个链接。可以通过索引节点号查找所有链接:
[root@studentvm1 ~]# find /usr -inum 531003
/usr/sbin/mkfs.ext3
/usr/sbin/mke2fs
/usr/sbin/mkfs.ext4
/usr/sbin/mkfs.ext2
也可以使用
-samefile
选项来实现相同的目的,而无需知道索引节点号:
[root@studentvm1 ~]# find /usr -samefile /usr/sbin/mkfs.ext3
/usr/sbin/mkfs.ext3
/usr/sbin/mke2fs
/usr/sbin/mkfs.ext4
/usr/sbin/mkfs.ext2
3. 符号链接(Symbolic Links)
在之前的实验中发现,硬链接不能跨文件系统使用。而符号链接(也称为软链接或 symlinks)可以解决这个问题。符号链接可以在大多数硬链接使用的地方使用,并且有更多的用途。
符号链接与硬链接的区别在于,硬链接直接指向文件的索引节点,而符号链接指向一个目录项,即一个硬链接。因此,符号链接不依赖于索引节点号,可以跨文件系统工作,并且可以指向目录本身。
以下是创建符号链接的实验步骤:
1. 以
student
用户身份,将
~/testdir
设为当前工作目录,创建一个符号链接并列出目录内容:
[student@studentvm1 testdir]$ ln -s link1 softlink1 ; ll
total 12
-rw-rw---- 4 student student 12 Feb 12 11:59 file001
-rw-rw---- 4 student student 12 Feb 12 11:59 link1
-rw-rw---- 4 student student 12 Feb 12 11:59 link2
lrwxrwxrwx 1 student student 5 Feb 12 12:37 softlink1 -> link1
可以使用
stat
命令验证符号链接和硬链接的不同:
[student@studentvm1 testdir]$ stat softlink1 link1
File: softlink1 -> link1
Size: 5 Blocks: 0 IO Block: 4096 symbolic link
Device: 253,4 Inode: 2183 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 1000/ student) Gid: ( 1000/ student)
Access: 2023-02-12 12:37:31.635884495 -0500
Modify: 2023-02-12 12:37:31.633884495 -0500
Change: 2023-02-12 12:37:31.633884495 -0500
Birth: 2023-02-12 12:37:31.633884495 -0500
File: link1
Size: 12 Blocks: 8 IO Block: 4096 regular file
Device: 253,4 Inode: 2182 Links: 4
Access: (0660/-rw-rw----) Uid: ( 1000/ student) Gid: ( 1000/ student)
Access: 2023-02-12 12:06:11.752916155 -0500
Modify: 2023-02-12 11:59:41.942185794 -0500
Change: 2023-02-12 12:09:29.392779392 -0500
Birth: 2023-02-12 11:59:41.942185794 -0500
符号链接有不同的时间戳、索引节点号和大小。
2. 从
/tmp
创建符号链接并验证内容:
[student@studentvm1 testdir]$ cd /tmp ; ln -s ~/testdir/file001 softlink2 ; ll /tmp
total 80
-rw-rw---- 1 student student 13 Feb 10 09:26 file09
drwx------. 2 root root 16384 Jan 17 07:29 lost+found
lrwxrwxrwx 1 student student 29 Feb 12 12:40 softlink2 -> /home/student/testdir/file001
drwx------ 2 student student 4096 Feb 8 14:35 ssh-XXXXXX2UEIbd
drwx------ 2 student student 4096 Feb 11 13:29 ssh-XXXXXXhSPk1A
<snip>
[student@studentvm1 tmp]$ cat softlink2
Hello World
- 删除原始文件,观察符号链接的变化:
[student@studentvm1 testdir]$ rm file001 ; ll
total 8
-rw-rw---- 3 student student 12 Feb 12 11:59 link1
-rw-rw---- 3 student student 12 Feb 12 11:59 link2
lrwxrwxrwx 1 student student 5 Feb 12 12:37 softlink1 -> link1
删除符号链接指向的硬链接会导致符号链接损坏。可以通过创建同名的新硬链接来修复损坏的符号链接,或者使用
rm
命令删除不再需要的符号链接。
unlink
命令也可以用于删除文件和链接。
3. 总结
通过以上实验,我们了解了硬链接和符号链接的特点和使用方法。硬链接适用于同一文件系统内的文件,而符号链接可以跨文件系统工作。在实际应用中,需要根据具体需求选择合适的链接类型。
以下是硬链接和符号链接的对比表格:
| 特性 | 硬链接 | 符号链接 |
| ---- | ---- | ---- |
| 跨文件系统 | 不支持 | 支持 |
| 指向对象 | 索引节点 | 目录项(硬链接) |
| 依赖索引节点号 | 是 | 否 |
| 可指向目录 | 否 | 是 |
| 元数据 | 与原文件相同 | 与原文件不同 |
下面是创建链接的流程图:
graph TD;
A[开始] --> B[创建文件];
B --> C{选择链接类型};
C -- 硬链接 --> D[使用ln命令创建硬链接];
C -- 符号链接 --> E[使用ln -s命令创建符号链接];
D --> F[验证硬链接属性];
E --> G[验证符号链接属性];
F --> H[结束];
G --> H;
4. 文件系统的定义
“文件系统”这个术语在不同的场景下有不同的含义,主要包括以下三种:
1.
特定的数据存储格式
:如 EXT3、EXT4、BTRFS、XFS 等。Linux 支持近 100 种文件系统类型,每种类型都有自己的元数据结构,用于定义数据的存储和访问方式。
2.
整个 Linux 分层目录结构
:从根目录(/)开始的整个目录体系。
3.
格式化后的分区或逻辑卷
:使用特定类型的文件系统格式化的分区或逻辑卷,可以挂载到 Linux 文件系统的指定挂载点上。
5. 数据存储在存储设备上的原因
每台通用计算机都需要将各种类型的数据存储在硬盘驱动器(HDD)、固态硬盘(SSD)或类似的存储设备(如 USB 闪存驱动器)上,主要有以下两个原因:
-
RAM 的易失性
:计算机断电后,RAM 中的内容会丢失。虽然有非易失性 RAM(如用于 USB 闪存驱动器和 SSD 的闪存 RAM),但普通 RAM 仍然是易失性的。
-
成本因素
:即使 RAM 和磁盘的成本都在迅速下降,但每字节的成本上,RAM 仍然比磁盘空间昂贵。例如,根据 16GB RAM 和 2TB 硬盘的成本计算,RAM 的单位成本约为硬盘的 71 倍。
6. 解决文件系统相关问题及操作
在 Linux 系统中,可能会遇到各种与文件系统相关的问题,以下是一些常见问题的解决方法和操作步骤:
6.1 查找具有特定硬链接数量的文件
可以使用
find
命令查找具有特定硬链接数量的文件。例如,查找具有四个硬链接的文件:
[root@studentvm1 ~]# find / -type f -links 4
6.2 查找特定文件名且具有特定硬链接数量的文件的索引节点号
[root@studentvm1 ~]# find / -type f -name mkfs*[0-9] -links 4 -exec ls -li {} \;
6.3 通过索引节点号查找文件
[root@studentvm1 ~]# find /usr -inum 531003
6.4 使用
-samefile
选项查找文件
[root@studentvm1 ~]# find /usr -samefile /usr/sbin/mkfs.ext3
6.5 创建符号链接
[student@studentvm1 testdir]$ ln -s link1 softlink1
6.6 从
/tmp
创建符号链接并验证内容
[student@studentvm1 testdir]$ cd /tmp ; ln -s ~/testdir/file001 softlink2
[student@studentvm1 tmp]$ cat softlink2
6.7 删除文件和链接
可以使用
rm
命令删除文件和链接,也可以使用
unlink
命令删除文件和链接。例如:
[student@studentvm1 testdir]$ rm file001
[student@studentvm1 testdir]$ unlink softlink1
7. 总结与建议
在 Linux 系统中,文件、目录和链接的管理是非常重要的。硬链接和符号链接各有特点,需要根据具体需求进行选择。同时,了解文件系统的不同定义和数据存储在存储设备上的原因,有助于更好地管理和维护系统。
以下是一些操作建议的列表:
1. 在创建硬链接时,要注意硬链接只能在同一文件系统内使用。
2. 使用符号链接时,要注意其指向的硬链接被删除或重命名会导致符号链接损坏。
3. 在查找文件时,可以根据需要使用
find
命令的不同选项。
下面是一个处理文件系统问题的流程图:
graph TD;
A[遇到文件系统问题] --> B{问题类型};
B -- 查找文件 --> C[使用find命令查找];
B -- 创建链接 --> D{选择链接类型};
D -- 硬链接 --> E[使用ln命令创建];
D -- 符号链接 --> F[使用ln -s命令创建];
B -- 删除文件或链接 --> G{选择删除命令};
G -- rm --> H[使用rm命令删除];
G -- unlink --> I[使用unlink命令删除];
C --> J[问题解决];
E --> J;
F --> J;
H --> J;
I --> J;
通过以上内容,我们对 Linux 系统中的文件、目录、链接和文件系统有了更深入的了解,可以更好地进行系统管理和维护。
Linux文件链接与文件系统解析
超级会员免费看

被折叠的 条评论
为什么被折叠?



