# 加軟連接,注意源文件為絕對地址,不然移動軟連接后,軟連接就會失效 ln -s 原文件絕對路徑 軟連接地址
# 刪除軟連接 rm -rf 軟連接地址 # 警告-警告-警告 注意這個“/”,如果軟連接是目錄,下面命令會刪除【軟連接目錄下的所有文件】,而不是刪除【軟連接】 rm -rf 軟連接地址/
---------------------------------------------------------------------------------------------------
先建立一個軟連接
[root@rekfan.com test ] # ls -il 總計 0 1491138 -rw-r–r– 1 root root 48 07-14 14:17 file1 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand #建立file1和file1soft軟連接 [root@rekfan.com test ] # ln -s file1 file1soft [root@rekfan.com test ] # ls -il 總計 0 1491138 -rw-r–r– 1 root root 48 07-14 14:17 file1 1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 file1soft -> file1 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand
刪除上面建立的軟連接
[root@rekfan.com test ] # ls -il 總計 0 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1 1491140 lrwxrwxrwx 1 root root 5 07-14 14:24 file1soft -> file1 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand #刪除軟連接 [root@rekfan.com test ] # rm -rf file1soft [root@rekfan.com test ] # ls -il 總計 0 1491138 -rw-r–r– 1 root root 0 07-14 14:17 file1 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2 1491139 -rw-r–r– 2 root root 0 07-14 14:17 file2hand 刪除軟鏈接 確實是用rm 但是!!! rm -fr xxxx/ 加了個/ 這個是刪除文件夾 rm -fr xxxx 沒有/ 這個是刪除軟鏈接
注意
源文件最好是絕對路徑,這樣移動軟鏈接就不會報錯
需要注意的是: 源文件的路徑 是根據 軟鏈接 的路徑來計算的,而不是根據當前路徑來計算的
創建軟連接,是非常簡單的,直接使用ln -s 命令即可,其語法為:ln -s 原始文件路徑 軟鏈接文件路徑。
比如, ln -s a.txt a_soft. 給a.txt創建了鏈接文件a_soft。
但是,如果當前所在目錄為/home. 要給該目錄下的a.txt文件創建一個軟連接a_soft,放在其子目錄B下,首先想到的是這樣:
ln -s ./a.txt ./B/a_soft
結果很不幸,這是錯誤的,創建出來的a_soft文件時找不到a.txt文件的。為什么呢??
這是因為,ln -s創建鏈接文件,如果原始文件路徑時相對路徑,其相對路徑的基准路徑為鏈接文件的路徑(這么理解,是通過鏈接文件找到源文件,因此就是以鏈接文件的路徑為當前路徑了)。因此,上面的鏈接文件路徑為B目錄,而源文件則認為是B目錄下的文件,因此當然錯誤了!
解決辦法
(1)源文件使用絕對路徑: ln -s /home/a.txt ./B/a_soft
(2)源文件使用相對路徑: ln -s ../a.txt ./B/a_soft (B目錄的上一級目錄是home目錄,目錄下存在a.txt)