输出:
hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ ln 2018.log ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ ll 总用量 20 drwxrwxr-x 3 hc hc 4096 10月 23 12:02 ./ drwxrwxr-x 6 hc hc 4096 10月 19 18:22 ../ -rw-r--r-- 2 hc hc 10 10月 23 11:57 2018.log drwxrwxr-x 2 hc hc 4096 10月 18 17:42 .idea/ lrwxrwxrwx 1 hc hc 8 10月 23 12:00 link2018 -> 2018.log -rw-r--r-- 2 hc hc 10 10月 23 11:57 ln2018说明:
为2018.log创建硬链接ln2018,2018.log与ln2018的各项属性相同
修改源文件2018.log内容,其软硬链接内容均会同步修改
hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat ln2018 我是log hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat 2018.log 我是log hc@hc-virtual-machine:~/PycharmProjects/my_test$ vim 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat 2018.log 我是log1 hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat ln2018 我是log1 hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat link2018 我是log1 3. 接上面两实例,链接完毕后,删除和重建链接原文件演示如下:
hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ ll 总用量 20 drwxrwxr-x 3 hc hc 4096 10月 23 12:04 ./ drwxrwxr-x 6 hc hc 4096 10月 19 18:22 ../ -rw-r--r-- 2 hc hc 11 10月 23 12:04 2018.log drwxrwxr-x 2 hc hc 4096 10月 18 17:42 .idea/ lrwxrwxrwx 1 hc hc 8 10月 23 12:00 link2018 -> 2018.log -rw-r--r-- 2 hc hc 11 10月 23 12:04 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ rm -rf 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test$ ll 总用量 16 drwxrwxr-x 3 hc hc 4096 10月 23 12:57 ./ drwxrwxr-x 6 hc hc 4096 10月 19 18:22 ../ drwxrwxr-x 2 hc hc 4096 10月 18 17:42 .idea/ lrwxrwxrwx 1 hc hc 8 10月 23 12:00 link2018 -> 2018.log -rw-r--r-- 1 hc hc 11 10月 23 12:04 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ touch 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test$ ll 总用量 16 drwxrwxr-x 3 hc hc 4096 10月 23 12:57 ./ drwxrwxr-x 6 hc hc 4096 10月 19 18:22 ../ -rw-r--r-- 1 hc hc 0 10月 23 12:57 2018.log drwxrwxr-x 2 hc hc 4096 10月 18 17:42 .idea/ lrwxrwxrwx 1 hc hc 8 10月 23 12:00 link2018 -> 2018.log -rw-r--r-- 1 hc hc 11 10月 23 12:04 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ vim 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat 2018.log 2018log日志 hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat link2018 2018log日志 hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat ln2018 我是log1 hc@hc-virtual-machine:~/PycharmProjects/my_test$说明:
源文件被删除后,并没有影响硬链接文件;软链接文件在centos系统下不断的闪烁,提示源文件已经不存在
重建源文件后,软链接不在闪烁提示,说明已经链接成功,找到了链接文件系统;重建后,硬链接文件并没有受到源文件影响,硬链接文件的内容还是保留了删除前源文件的内容,说明硬链接已经失效
4. 将文件链接到目录中演示如下:
hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 hc@hc-virtual-machine:~/PycharmProjects/my_test$ mkdir test hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 test hc@hc-virtual-machine:~/PycharmProjects/my_test$ ln 2018.log test hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 test hc@hc-virtual-machine:~/PycharmProjects/my_test$ cd test/ hc@hc-virtual-machine:~/PycharmProjects/my_test/test$ ls 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test/test$ vi 2018.log hc@hc-virtual-machine:~/PycharmProjects/my_test/test$ cat 2018.log 2018log日志,加1 hc@hc-virtual-machine:~/PycharmProjects/my_test/test$ cd .. hc@hc-virtual-machine:~/PycharmProjects/my_test$ ls 2018.log link2018 ln2018 test hc@hc-virtual-machine:~/PycharmProjects/my_test$ cat 2018.log 2018log日志,加1 hc@hc-virtual-machine:~/PycharmProjects/my_test$ ll 总用量 24 drwxrwxr-x 4 hc hc 4096 10月 23 13:31 ./ drwxrwxr-x 7 hc hc 4096 10月 23 13:30 ../ -rw-r--r-- 2 hc hc 19 10月 23 13:32 2018.log drwxrwxr-x 2 hc hc 4096 10月 18 17:42 .idea/ lrwxrwxrwx 1 hc hc 8 10月 23 12:00 link2018 -> 2018.log -rw-r--r-- 1 hc hc 11 10月 23 12:04 ln2018 drwxr-xr-x 2 hc hc 4096 10月 23 13:32 test/ hc@hc-virtual-machine:~/PycharmProjects/my_test$说明:
在test目录中创建了2018.log的硬链接,修改test目录中的2018.log文件,同时也会同步到源文件
5:给目录创建软链接命令:
ln -sv 源目录 目标目录