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.www58058.com/53450

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

相關推薦

  • vsftpd虛擬用戶搭建

    首選的FTP服務器搭建方式 安裝vsftpd      yum install -y vsftpd      systemctl enable vsftpd     #設置開機啟動      systemctl is-enabled vsf…

    Linux干貨 2017-04-27
  • rpm和yum常用命令選項總結

    =======rpm命令的使用總結====== 安裝:  rpm {-i|–install} [install-options] PACKAGE_FILE …     -v: verbose     -vv: more verbose &nbsp…

    Linux干貨 2015-12-15
  • ?文件服務器 – vsftpd

    文件服務器 – vsftpd 概述 FTP協議 vsftpd的配置文件 vsftpd + pam + file_db + virtual_vsftp_user vsftpd + pam + mysql + virtual_vsftp_user 登錄時錯誤類型 總結 文件服務器是一種專供其他電腦訪問文件和存儲的程序。文件服務器通常比一般的個人電腦擁…

    Linux干貨 2016-10-15
  • 關于大型網站技術演進的思考(五):存儲的瓶頸(5)

    原文出處: 夏天的森林    上文里我遺留了兩個問題,一個問題是數據庫做了水平拆分以后,如果我們對主鍵的設計采取一種均勻分布的策略,那么它對于被水平拆分出的表后續的查詢操作將有何種影響,第二個問題就是水平拆分的擴容問題。這兩個問題在深入下去,本系列就越來越技術化了,可能最終很多朋友讀完后還是沒有找到解決實際問題的啟迪,而且我覺得…

    Linux干貨 2015-03-11
  • vim文本操作、例行性工作、腳本編程_第六周練習(01)

    vim文本操作 Q1:復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; [root@promote /]# cp /etc/rc.d/rc.sysinit /tmp/ [root@promote /]# l…

    Linux干貨 2016-12-18
  • Linux包管理:rpm/yum/編譯安裝

    rpm:安裝,查詢,升級,校驗,卸載   Topic: 程序包 rpm管理 yum管理   程序包:   什么是程序包管理器: 將編譯好的應用程序的各個組成文件打包成一個或幾個程序包文件,從而更方便地實現程序包的安裝、升級、卸載和查詢等管理操作   程序包的組成清單: 文件清單 安裝或卸載時運行的腳本 數據庫(公共) …

    2017-09-14
欧美性久久久久