一、软链接和硬链接
1、软链接
符号链接又叫软链接,和原文件不是一个文件 例如Windows的快捷方式,如果原始文件被删除,所有
指向它的符号链接也就都被破坏了。软链接有自己的node,是linux特殊文件的一种,作为一个文件,
它的数据是它所连接的文件的路径。符号链接可以跨越文件系统,也可以为目录建立。
创建软链接文件ln -s 原文件 目标文件
图解
样例:
[root@ls_nfqZ8Onc home]# mkdir /home/data
[root@ls_nfqZ8Onc home]# cd /home/data
[root@ls_nfqZ8Onc data]# touch file
[root@ls_nfqZ8Onc data]# ln -s file file_copy
[root@ls_nfqZ8Onc data]# ls
file file_copy
[root@ls_nfqZ8Onc data]# stat file
File: file
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 656900 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-14 17:45:48.083449607 +0800
Modify: 2022-07-14 17:45:48.083449607 +0800
Change: 2022-07-14 17:45:48.083449607 +0800
Birth: -
[root@ls_nfqZ8Onc data]# stat file_copy
File: file_copy -> file
Size: 4 Blocks: 0 IO Block: 4096 symbolic link
Device: fd01h/64769d Inode: 656901 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-14 17:46:46.876686098 +0800
Modify: 2022-07-14 17:46:41.575664771 +0800
Change: 2022-07-14 17:46:41.575664771 +0800
Birth: -
当删除原文件时:
[root@ls_nfqZ8Onc data]# rm -r file
rm: remove regular empty file 'file'? y
[root@ls_nfqZ8Onc data]# ls
file_copy
[root@ls_nfqZ8Onc data]# ls -l
total 0
lrwxrwxrwx 1 root root 4 Jul 14 17:46 file_copy -> file
2、硬链接
硬链接 只能引用同一文件系统中的文件。它引用的是文件在文件系统中的物理索引(也称为inode)。
当移动或者删除原始文件时,硬链接不会被破坏,因为它所引用的是文件的物理数据而不是文件在件结
构中的位置
创建硬链接文件:ln 原文件 目标文件
图解
[root@ls_nfqZ8Onc data]# ln file file_copy1
[root@ls_nfqZ8Onc data]# stat file
File: file
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 656900 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-14 18:13:04.462817371 +0800
Modify: 2022-07-14 18:13:04.462817371 +0800
Change: 2022-07-14 18:13:19.030870719 +0800
Birth: -
[root@ls_nfqZ8Onc data]# stat file_copy1
File: file_copy1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 656900 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-14 18:13:04.462817371 +0800
Modify: 2022-07-14 18:13:04.462817371 +0800
Change: 2022-07-14 18:13:19.030870719 +0800
Birth: -
[root@ls_nfqZ8Onc data]# rm -r file
rm: remove regular empty file 'file'? y
[root@ls_nfqZ8Onc data]# ls
file_copy1
[root@ls_nfqZ8Onc data]# stat file_copy1
File: file_copy1
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 656900 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-14 18:13:04.462817371 +0800
Modify: 2022-07-14 18:13:04.462817371 +0800
Change: 2022-07-14 18:27:58.162043692 +0800
Birth: -