歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(30)ln命令

每天一個Linux命令(30)ln命令

日期:2017/3/3 12:19:48   编辑:Linux技術

[b]ln命令用來為文件創建鏈接,連接類型分為硬鏈接和符號鏈接兩種,默認的連接類型是硬連接。如果要創建符號連接必須使用"-s"選項。[/b] [b] (1)用法:[/b]

[b]用法: ln [options] source dist[/b] [b] (2)功能:[/b]

[b]功能: 在文件之間建立連接 [/b] 注意: 符號鏈接文件不是一個獨立的文件,它的許多屬性依賴於源文件,所以給符號鏈接文件設置存取權限是沒有意義的。

[b] (3)選項參數:[/b] 1) -s          軟鏈接(符號鏈接)

2) -v          顯示詳細的處理過程 3) -d          允許超級用戶制作目錄的硬鏈接

[b] (4)實例:[/b]

1)[root@localhost Documents]# ln -s findDir finDir_link        為目錄創建軟連接

[root@localhost Documents]# ll
總用量 0
dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir
[root@localhost Documents]# ln -s findDir finDir_link
[root@localhost Documents]# ll
總用量 0
dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
lrwxrwxrwx. 1 root root       7 5月  27 06:04 finDir_link -> findDir
drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir

當源文件失效後,鏈接文件將失效。

[root@localhost Documents]# ll
總用量 0
dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
lrwxrwxrwx. 1 root root       7 5月  27 06:04 finDir_link -> findDir    //有效時的顏色
drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir
[root@localhost Documents]# cd finDir_link
[root@localhost finDir_link]# ll
總用量 0
dr-xr-xr-x. 3 root sunjimeng 60 5月  24 08:01 Dir
[root@localhost findDir]# rmdir Dir
[root@localhost findDir]# cd ../
[root@localhost Documents]# rmdir findDir
[root@localhost Documents]# ll
總用量 0                                   //無效時的顏色
lrwxrwxrwx. 1 root root  7 5月  27 06:04 finDir_link -> findDir 
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# cd finDir_link
bash: cd: finDir_link: 沒有那個文件或目錄

2)[root@localhost Documents]# ln newFile newLink          給文件創建硬鏈接

[root@localhost Documents]# ll
總用量 0
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# touch newFile             //創建文件
[root@localhost Documents]# ln -s newFile newLink_s   //創建文件符號鏈接
[root@localhost Documents]# ln newFile newLink        //創建文件硬鏈接
[root@localhost Documents]# ln -s Pdir PdirLink_s     //創建目錄符號鏈接
[root@localhost Documents]# ln Pdir PdirLink          //不允許創建目錄硬鏈接
ln: "Pdir": 不允許將硬鏈接指向目錄
[root@localhost Documents]# ll
總用量 0
-rw-r--r--. 2 root root  0 5月  27 06:18 newFile
-rw-r--r--. 2 root root  0 5月  27 06:18 newLink          
lrwxrwxrwx. 1 root root  7 5月  27 06:19 newLink_s -> newFile
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
lrwxrwxrwx. 1 root root  4 5月  27 06:19 PdirLink_s -> Pdir

創建的文件硬鏈接newLink與源文件newFile具有相同的權限,並且沒有箭頭。而文件軟鏈接newLink_s的權限要多得多,而且有指向符號。 3)綜合實例,比較硬鏈接與符號鏈接的差別

[root@localhost Documents]# cat >newFile <<EOF
> This is original file!
> 
> I'm test the hard link and the symbol link!
> EOF                                                     //到這裡新建一個文件
總用量 4
[root@localhost Documents]# ln -s newFile newFile_link_s
[root@localhost Documents]# ln newFile newFile_link
[root@localhost Documents]# rm newFile                   //刪除源文件
rm:是否刪除普通文件 "newFile"?y
[root@localhost Documents]# ll
總用量 4
-rw-r--r--. 1 root root 68 5月  27 06:30 newFile_link    
lrwxrwxrwx. 1 root root  7 5月  27 06:31 newFile_link_s -> newFile
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# cat newFile_link              //查看硬鏈接,完全不受影響,但符號鏈接已經失效
This is original file!

I'm test the hard link and the symbol link!
[root@localhost Documents]# cat >newFile <<EOF            再新建一個文件newFile   
> The Second Test!
> 
> EOF
[root@localhost Documents]# ll
總用量 8
-rw-r--r--. 1 root root 18 5月  27 06:33 newFile
-rw-r--r--. 1 root root 68 5月  27 06:30 newFile_link
lrwxrwxrwx. 1 root root  7 5月  27 06:31 newFile_link_s -> newFile   //符號鏈接已經恢復
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# cat newFile_link            //分別查看符號鏈接和硬鏈接發現硬鏈接內容不變,符號鏈接內容變為新建的文件內容了。
This is original file!

I'm test the hard link and the symbol link!
[root@localhost Documents]# cat newFile_link_s
The Second Test!
4)[root@localhost Documents]# ln newFile ln_dir          在另一個目錄創建同名硬鏈接

[root@localhost Documents]# mkdir ln_dir
[root@localhost Documents]# ln newFile ln_dir
[root@localhost Documents]# cd ln_dir
[root@localhost ln_dir]# ll
總用量 4
-rw-r--r--. 2 root root 18 5月  27 06:33 newFile
修改newFile硬鏈接目錄文件,也會導致源文件被修改。

5)[root@localhost Documents]# ln -sv a.c ./Pdir           在指定目錄創建鏈接

[root@localhost Documents]# touch a.c
[root@localhost Documents]# ll
總用量 0
-rw-r--r--. 1 root root  0 5月  27 07:03 a.c
lrwxrwxrwx. 1 root root  6 5月  27 06:58 No_link -> NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
[root@localhost Documents]# ln -sv a.c ./Pdir
"./Pdir/a.c" -> "a.c"
[root@localhost Documents]# ln -sv a.c ./Pdir/b.c
"./Pdir/b.c" -> "a.c"
[root@localhost Documents]# ln -v a.c ./Pdir/c.c
"./Pdir/c.c" => "a.c"
[root@localhost Documents]# ls -l Pdir
總用量 8
lrwxrwxrwx. 1 root root   3 5月  27 07:04 a.c -> a.c
lrwxrwxrwx. 1 root root   3 5月  27 07:04 b.c -> a.c
-rw-r--r--. 2 root root   0 5月  27 07:03 c.c
-r--r--r--. 1 root root   0 5月  19 04:16 find
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root   0 5月  15 18:34 uText
-rw-r--r--. 1 root root 105 5月  21 06:35 vf

[b] (5)其他:[/b] 擴展知識:

Linux具有為一個文件起多個名字的功能,稱為鏈接。被鏈接的文件可以存放在相同的目錄下,但是必須有不同的文件名,而不用在硬盤上為同樣的數據重復備份。另外,被鏈接的文件也可以有相同的文件名,但是存放在不同的目錄下,這樣只要對一個目錄下的該文件進行修改,就可以完成對所有目錄下同名鏈接文件的修改。對於某個文件的各鏈接文件,我們可以給它們指定不同的存取權限,以控制對信息的共享和增強安全性。 文件鏈接有兩種形式,即硬鏈接和符號鏈接。硬鏈接:

建立硬鏈接時,在另外的目錄或本目錄中增加目標文件的一個目錄項,這樣,一個文件就登記在多個目錄中。 創建硬鏈接後,己經存在的文件的I節點號(Inode)會被多個目錄文件項使用。一個文件的硬鏈接數可以在目錄的長列表格式的第二列中看到,無額外鏈接的文件的鏈接數為l。 在默認情況下,ln命令創建硬鏈接。ln命令會增加鏈接數,rm命令會減少鏈接數。一個文件除非鏈接數為0,否則不會從文件系統中被物理地刪除。

對硬鏈接有如下限制: 1.不能對目錄文件做硬鏈接。

2.不能在不同的文件系統之間做硬鏈接。就是說,鏈接文件和被鏈接文件必須位於同一個文件系統中。軟鏈接:

符號鏈接也稱為軟鏈接,是將一個路徑名鏈接到一個文件。這些文件是一種特別類型的文件。事實上,它只是一個文本文件,其中包含它提供鏈接的另一個文件的路徑名,如圖中虛線箭頭所示。另一個文件是實際包含所有數據的文件。所有讀、寫文件內容的命令被用於符號鏈接時,將沿著鏈接方向前進來訪問實際的文件。

與硬鏈接不同的是,符號鏈接確實是一個新文件,當然它具有不同的I節點號;而硬鏈接並沒有建立新文件。 符號鏈接沒有硬鏈接的限制,可以對目錄文件做符號鏈接,也可以在不同文件系統之間做符號鏈接。用ln -s命令建立符號鏈接時,源文件最好用絕對路徑名。這樣可以在任何工作目錄下進行符號鏈接。而當源文件用相對路徑時,如果當前的工作路徑與要創建的符號鏈接文件所在路徑不同,就不能進行鏈接。 符號鏈接保持了鏈接與源文件或目錄之間的區別: 刪除源文件或目錄,只刪除了數據,不會刪除鏈接。一旦以同樣文件名創建了源文件,鏈接將繼續指向該文件的新數據。 在目錄長列表中,符號鏈接作為一種特殊的文件類型顯示出來,其第一個字母是l。 符號鏈接的大小是其鏈接文件的路徑名中的字節數。

Copyright © Linux教程網 All Rights Reserved