歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux ln命令學習

Linux ln命令學習

日期:2017/2/28 14:37:19   编辑:Linux教程

ln命令用來給檔案創建鏈接。

1. 使用ln命令給檔案創建symbolic link。
linux系統下的symbolic link類似於windows系統的快捷方式一樣。
使用ls命令查看,可以看到新創建的symbolic link有獨立的inode,也就是symbolic link會占用一個inode,但是其實際內容仍然是指向
源文件所指向的block區域。
# touch /tmp/file
# ls -lhi /tmp/file
3441 -rw-r--r-- 1 root root 0 Jan 1 00:09 /tmp/file
#
# ln -fs /tmp/file /tmp/symbolic_link
# ls -lhi /tmp/symbolic_link
3647 lrwxrwxrwx 1 root root 9 Jan 1 00:10 /tmp/symbolic_link -> /tmp/file
#

2. 刪除symbolic link文件,實際上就是刪除這個inode,而不會影響到源文件所指向的block區域;
而如果刪除了源文件,那這個symbolic link文件就基本上沒用了。
# echo "link test" > /tmp/file
# cat /tmp/file
link test
#
# rm /tmp/symbolic_link
# cat /tmp/file
link test
#
# ln -fs /tmp/file /tmp/symbolic_link
#
# rm /tmp/file
# cat /tmp/symbolic_link
cat: can't open '/tmp/symbolic_link': No such file or directory
#
# ls -lhi /tmp/symbolic_link
7357 lrwxrwxrwx 1 root root 9 Jan 1 00:22 /tmp/symbolic_link -> /tmp/file
#

3. 使用ln命令創建hard link。
可以看到,創建hard link是使用同一個inode,而copy了一份源文件的block區域出來。
如果修改檔案的內容,源文件和hard link文件對應的block區域內容都會被修改,從而保持一致性。
# touch /tmp/file
# echo "hard link test" > /tmp/file
# cat /tmp/file
hard link test
# ln /tmp/file /tmp/hard_link
# ls -lhi /tmp/file
7996 -rw-r--r-- 2 root root 15 Jan 1 00:25 /tmp/file
# ls -lhi /tmp/hard_link
7996 -rw-r--r-- 2 root root 15 Jan 1 00:25 /tmp/hard_link
# cat /tmp/hard_link
hard link test
#
# echo "hard link test 2" > /tmp/file
# cat /tmp/file
hard link test 2
# cat /tmp/hard_link
hard link test 2
#
# echo "hard link test 3" > /tmp/hard_link
# cat /tmp/file
hard link test 3
# cat /tmp/hard_link
hard link test 3
#

4. 刪除hard link或者刪除源文件,實際上只是刪除其中其中一份block區域。
可以看到,雖然源文件被刪除(實際上只是刪除了源文件對應的block區),但是
inode仍然還在,所以仍然可以透過hard link檔案來訪問源文件的內容。
到了這裡,就可以理解為什麼inode信息中不包含文件名了;
因為如果文件名信息包含在inode中,並且創建了hard link,此時為何還需要兩塊不同的block區域
來儲存文件信息呢?進而hard link還有什麼意義呢?
# rm /tmp/file
# cat /tmp/file
cat: can't open '/tmp/file': No such file or directory
#
# cat /tmp/hard_link
hard link test 3
#
# ls -hli /tmp/hard_link
7996 -rw-r--r-- 1 root root 17 Jan 1 00:29 /tmp/hard_link
#

5. 為目錄創建symbolic link?
因為新建的symbolic link目錄與源目錄是同一個inode,所以對這兩個目錄的訪問具有完全相同的表現。
# mkdir /tmp/directory
# ln -fs /tmp/directory/ /tmp/dir_sym_link
#
# ls -hdi /tmp/directory/
14018 /tmp/directory/
# ls -hdi /tmp/dir_sym_link/
14018 /tmp/dir_sym_link/
#
# touch /tmp/directory/file
# ls -hil /tmp/directory/file
14781 -rw-r--r-- 1 root root 0 Jan 1 00:47 /tmp/directory/file
# ls -hil /tmp/dir_sym_link/file
14781 -rw-r--r-- 1 root root 0 Jan 1 00:47 /tmp/dir_sym_link/file
#
# echo "directory symbolic test" > /tmp/dir_sym_link/file
# cat /tmp/dir_sym_link/file
directory symbolic test
# cat /tmp/directory/file
directory symbolic test
#

6. 為目錄創建hard link?
從結果看,為目錄創建hard link失敗了。
# ln /tmp/directory/ /tmp/dir_hard_link
ln: /tmp/dir_hard_link: Operation not permitted

待確認問題:
1. 文件名信息既然不是存儲在inode中,那是存儲在哪的呢?
2. 為目錄創建symbolic link,竟然和源目錄文件是同一個inode,目錄有什麼特殊性?

推薦閱讀:

Linux下學習ln命令筆記 http://www.linuxidc.com/Linux/2012-08/67847.htm

Linux上ln命令詳解 http://www.linuxidc.com/Linux/2013-01/77624.htm

Linux上ln命令詳細使用說明 http://www.linuxidc.com/Linux/2012-09/70203.htm

Linux ln命令參數及用法詳解---Linux連接文件或目錄命令 http://www.linuxidc.com/Linux/2012-06/61850.htm

Linux ln命令的注意事項 http://www.linuxidc.com/Linux/2011-10/44549.htm

Copyright © Linux教程網 All Rights Reserved