目次
在本指南中,我将先容什么是链接以及如何更好地使用它们以及所需的概念。 通过执行 man ln 下令,可以看到这是在文件之间建立链接,而没有提及是软链接或硬链接。 [code]shashi@linuxtechi ~}$ man ln[/code]雷同地,下令 man link 形貌为调用链接功能创建文件。 Soft Link软链接顾名思义就是创建到新文件的新链接。在这种情况下,新文件的节点号将指向旧文件。 Hard Link在这种情况下,旧文件和新文件都将指向雷同的索引节点号。 Symbolic Link在某些 Unix/Linux 体系中,符号和软链接都被视为雷同的。但是实际的差别是,新的文件和旧文件将指向一个新的 Inode 号码,这将完全取决于实行。 注意: 在许多情况下,符号和软链接术语可以交换使用,但是人们必须知道什么时间用什么。 Creating Hard Link(1) man ln 下令输出如下 [code]shashi@linuxtechi ~}$ man ln ln - make links between files[/code](2) ln 下令无参数,输出如下 [code]shashi@linuxtechi ~}$ln ln: missing file operand Try 'ln --help' for more information.[/code](3) 在两个文件之间创建硬链接,起首查抄文件是否存在,否则将创建文件,然后链接它们。 [code]shashi@linuxtechi ~}$ ls -la total 24 drwx------ 4 root root 4096 Feb 6 15:23 . drwxr-xr-x 23 root root 4096 Jan 25 16:39 .. -rw-r--r-- 1 root root 3122 Jan 25 16:41 .bashrc drwx------ 2 root root 4096 Feb 6 15:23 .cache -rw-r--r-- 1 root root 0 Jan 25 16:41 .hushlogin -rw-r--r-- 1 root root 148 Aug 17 2015 .profile drwxr-xr-x 2 root root 4096 Jan 25 16:41 .ssh[/code](4) 使用 touch 下令创建文件 [code]shashi@linuxtechi ~}$ touch 123.txt shashi@linuxtechi ~}$ ls -l total 0 -rw-r--r-- 1 root root 0 Feb 6 15:51 123.txt[/code](5) 使用 cat 下令将内容输入到文件中,然后按 ctrl+c 保存并退出。 [code]shashi@linuxtechi ~}$ cat > 123.txt Welcome to this World! ^C[/code](6) 创建 123.txt 和 321.txt 文件之间的硬链接 [code]shashi@linuxtechi ~}$ ln 123.txt 321.txt shashi@linuxtechi ~}$ ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt[/code](7) 查抄文件的索引节点号 [code]shashi@linuxtechi ~}$ ls -li total 8 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt $ cat 321.txt Welcome to this World 再创建一个名为 456.txt 的文件,并使用 ln 下令将其链接到 321.txt [code]shashi@linuxtechi ~}$ ls -li total 12 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt 794583 -rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt $ cat 456.txt Welcome to this World! shashi@linuxtechi ~}$ ls -l total 12 -rw-r--r-- 3 root root 23 Feb 6 15:52 123.txt -rw-r--r-- 3 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 3 root root 23 Feb 6 15:52 456.txt[/code](9) 当源文件或这些文件中的任何一个被删除时,它不会影响其他文件。 [code]shashi@linuxtechi ~}$ rm 123.txt shashi@linuxtechi ~}$ ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ ls -li total 8 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt 794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ cat 456.txt Welcome to this World 不答应跨目次创建硬链接。 [code]shashi@linuxtechi ~}$ls -l total 8 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt -rw-r--r-- 2 root root 23 Feb 6 15:52 456.txt shashi@linuxtechi ~}$ mkdir abc shashi@linuxtechi ~}$ ln abc def ln: abc: hard link not allowed for directory[/code](11) 对一个文件内容的任何更改都将影响并相应地更改其他文件的内容。 [code]shashi@linuxtechi ~}$ vi 321.txt Welcome to this World! You are welcomed to this new world :wq shashi@linuxtechi ~}$ ls -l total 12 -rw-r--r-- 2 root root 59 Feb 6 16:24 321.txt -rw-r--r-- 2 root root 59 Feb 6 16:24 456.txt drwxr-xr-x 2 root root 4096 Feb 6 16:18 abc shashi@linuxtechi ~}$ cat 456.txt Welcome to this World! You are welcomed to this new world[/code]Creating Soft Link:(1) 使用 touch 下令创建文件 src.txt,使用 cat 下令输入内容,然后按 ctrl+c 保存并退出。 [code]shashi@linuxtechi ~}$ touch src.txt shashi@linuxtechi ~}$ cat > src.txt Hello World ^C shashi@linuxtechi ~}$ ls -l total 4 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt[/code](2) 将目标文件创建为 dst.txt,并使用 ln -s 下令创建符号链接 (也称为软链接)。查看 dst.txt 文件的内容,可以看到与 src.txt 雷同的内容。 [code]shashi@linuxtechi ~}$ ln -s src.txt dst.txt shashi@linuxtechi ~}$ ls -l total 4 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ cat dst.txt Hello World[/code](3) 在符号链接的情况下,源文件和目标文件的 inode 号差别。在权限中出现字母 l,表明这些是链接。dst.txt ->src.txt 将是现在建立的新链接。 [code]shashi@linuxtechi ~}$ ls -li total 4 794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt 794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt[/code](4) 答应对目次举行符号创建 [code]shashi@linuxtechi ~}$ mkdir abc shashi@linuxtechi ~}$ ln -s abc def $ ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt[/code](5) 源和目标的 Inode 编号差别 [code]shashi@linuxtechi ~}$ ls -li total 8 794585 drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc 794586 lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc 794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt 794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt[/code](6) 如前所述,可以为目次创建符号链接。一旦创建了这些带有符号链接的目次,就可以在这些目次中创建文件。当在源目次中创建文件时,同样的情况也会反映在目标目次中。 [code]shashi@linuxtechi ~}$ $ cd abc shashi@linuxtechi ~}$ touch 123.txt shashi@linuxtechi ~}$ vi 123.txt Hello :wq! shashi@linuxtechi ~}$ touch 456.txt shashi@linuxtechi ~}$ cd .. shashi@linuxtechi ~}$ ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 6 16:36 abc lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ cd def shashi@linuxtechi ~}$ ls -l total 4 -rw-r--r-- 1 root root 6 Feb 6 16:37 123.txt -rw-r--r-- 1 root root 0 Feb 6 16:36 456.txt shashi@linuxtechi ~}$ cat 123.txt Hello[/code]Removing Soft Link / Symbolic Links使用 rm 和 unlink 下令删除软链接或符号链接 [code]# rm <soft-link-filename> # unlink <soft-link-filename>[/code]删除软链接目次 [code]# rm <soft-link-directory> # unlink <soft-link-directory>[/code]以上就是在Linux体系上创建软毗连和硬毗连的方法的具体内容,更多关于Linux创建软毗连和硬毗连的资料请关注脚本之家别的相干文章! 来源:https://www.jb51.net/server/326388h6s.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 19:42 , Processed in 0.031612 second(s), 18 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.