cp复制软链接失效的原因

在学习cp命令时我们会知道复制软链接时,如果要保留链接文件使用-d,但当我们实际操作时却常常出现如下
情况

[root@localhost ~]# ls -l /etc/redhat-release     #此文件为链接文件 lrwxrwxrwx. 1 root root 14 Oct 17 08:48 /etc/redhat-release -> centos-release  
[root@localhost ~]# cp -d /etc/redhat-release /root/redhat.s   #使用-d 保存链接文件属性 
[root@localhost ~]# ls -l /root/redhat.s            #查看可知centos-release是闪烁的,也就是不存在 
lrwxrwxrwx 1 root root 14 Oct 22 09:06 /root/redhat.s -> centos-release  
[root@localhost ~]# cat /root/redhat.s    #看不了/root/redhat.s链接文件 cat: /root/redhat.s: No such file or directory

为什么我们使用cp -d 复制链接文件会失败?那就要看cp -d 是如何复制软链接的。在上例中,我们打算将
/etc/redhat-release软链接复制到/root/redhat.s。在使用cp -d实际上是将原软链接中指针区代表原文件
路径的字符串拷贝到新创建的/root/redhat.s中
下面命令可以看出其文件大小都为14,即centos-release
字符串大小

[root@localhost ~]# ls -l /etc/redhat-release /root/redhat.s  
lrwxrwxrwx. 1 root root 14 Oct 17 08:48 /etc/redhat-release -> centos-release lrwxrwxrwx  
1 root root 14 Oct 22 09:06 /root/redhat.s -> centos-release

当我们查看新建软链接/root/redhat.s时看到原文件路径是centos-release,就会到当前路径(/root/)
上查看centos-release文件,而实际上原文件在/etc/目录下,所以查看/root/redhat.s才会出现
/root/redhat.s: No such file or directory的错误。
根据这个原理我们可以推断,如果原链接文件存的是原文件的绝对路径的话,新建的链接文件就能找到原文件而不是报错,接下来我们实验一下。

#1. 先创建一个软链接/etc/redhat-release2,可知数据区存的是原文件的绝对路径/etc/centos-release 
[root@localhost ~]# ln -s /etc/centos-release /etc/redhat-release2 
[root@localhost ~]# ls -l /etc/redhat-release2 lrwxrwxrwx 1 root root 19 Oct 22 09:37 /etc/redhat-release2 -> /etc/centos-release  
#2. 复制刚创建的软链接/etc/redhat-release2到/root/radhat2.s 
[root@localhost ~]# cp -d /etc/redhat-release2 /root/radhat2.s 
[root@localhost ~]# ls -l /root/radhat2.s lrwxrwxrwx 1 root root 19 Oct 22 09:41 /root/radhat2.s -> /etc/centos-release  
#3. 查看新软链接内容 
[root@localhost ~]# cat /root/radhat2.s CentOS release 6.7 (Final)

cp -d 复制软链接时总结

1. 当原链接文件中存的是原文件的绝对路径:创建的新链接文件可在任何路径下。
2. 当原链接文件中存的是原文件的相对路径:
    例1:
        新链接文件在/root/link2
        相对路径是 centos (来自原链接文件的指针数据区)
        那么/root/centos就是原文件路径
    例2:
        新链接文件为/root/test/link2
        相对路径为../centos
        那么/root/centos为原文件路径
    原文件路径找得到就ok,找不到就报错。

原创文章,作者:lirou,如若转载,请注明出处:http://www.178linux.com/53450

(3)
liroulirou
上一篇 2016-10-25 09:46
下一篇 2016-10-25 09:46

相关推荐

  • linux 文件权限以及用户策略 讲解

     linux 文件权限以及用户策略 讲解    由于linux系统是一个多用户使用的系统,对于各个用户指定的文件或目录必须存在一套管理系统,以防止多用户对相同文件的混淆使用。于是催生出了linux系统文件的用户权限设置。其存在的意义就是每一个文件或目录对于不同的用户区分读取,写入,执行三种权限,即:r,w,x。下面将详…

    Linux干货 2016-08-05
  • shell变量的浅谈

    变量本质上是存储数据的一个或多个计算机内存地址,变量的命令规则包括: 1) 不能使用程序中的保留字,如if, for 2) 变量由字母、下划线和数字组成,且不能以数字开头 3) 要求风名知义 4) 统一命名规则:驼峰命名法 变量主要分为本地变量、环境变量、局部变量、位置变量和特殊变量 (1)本地变量:只对当前shell…

    2017-08-05
  • N24_小天 学习宣言

    每周安排时间看视频,每天安排时间学习并跟进课程,认真记笔记,认真写博客。多动手操作,坚持不断。学习思维,提升自己解决问题的思路。多动手,多实践。

    Linux干货 2016-10-27
  • 文件的从属权限和特殊权限

    基础权限   rwx     经过今天的洗礼,了解了文件的权限位(rwx),而权限对于目录和文件有着不同的意义     使用ls -l 命令后可以看到     -rw-r–r–. 1 root root 1…

    Linux干货 2016-08-04
  • LINUX用户建立秘钥认证实现SHELL脚本管理,分发,部署

    环境介绍 SSH Server [root@vm1 ~]# ifconfig |awk '/broadcast/{print $2}' 192.168.99.241 SSH Client [root@vm2 ~]# ifconfig |awk '…

    Linux干货 2017-01-05
  • 数据流重导向

    数据流重导向(redirect):就是将某个指令执行后应该要出现在屏幕上的数据,传输到其他的位置。 standard output:标准输出是指指令执行回传正确的讯息。 standard error output : 指令执行失败后,所回传的错误讯息。 1标准输入(stdin):代码为0,使用< <<2标准输出(stdout),代码为1,使…

    Linux干货 2016-08-08