Linux建立軟鏈接、硬鏈接

軟鏈接

說明:軟鏈接僅僅包含所鏈接文件的路徑名,因此能鏈接目錄文件,也可以跨越文件系統進行鏈接。但是,當原始文件被刪除後,鏈接文件也將失效。
1.軟鏈接,以路徑的形式存在。類似於Windows操作系統中的快捷方式
2.軟鏈接可以跨文件系統 ,硬鏈接不可以
3.軟鏈接可以對一個不存在的文件名進行鏈接
4.軟鏈接可以對目錄進行鏈接

建立軟鏈接:ln -s 源文件或目錄 目標文件或目錄

示例:

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

linux下的軟鏈接類似於windows下的快捷方式
-s 是 symbolic的意思

刪除軟鏈接:rm –rf 軟鏈接名稱

rm -rf b(請注意不要在後面加”/”,rm -rf 後面加“/”的話,會進行遞歸刪除,非常危險!)
示例:

rm -rf /usr/bin/ngnix
修改軟鏈接:ln –snf 新的源文件或目錄 目標文件或目錄

將會修改原有的鏈接地址爲新的地址,示例:
創建一個軟鏈接:

ln –s  /var/www/test   /var/test

修改指向的新路徑:

ln –snf  /var/www/test1   /var/test
找到文件夾下所有建立的軟鏈接:ls -alR | grep ^l

示例:

[root@CentOS6 home]# ls -alR | grep ^l
lrwxrwxrwx. 1 root root   22 Mar 16 17:59 aes.h -> ../../crypto/aes/aes.h
lrwxrwxrwx. 1 root root   24 Mar 16 17:59 asn1.h -> ../../crypto/asn1/asn1.h
lrwxrwxrwx. 1 root root   28 Mar 16 17:59 asn1_mac.h -> ../../crypto/asn1/asn1_mac.h
以下省略

硬鏈接

說明:可以將硬鏈接理解爲一個“指向原始文件inode的指針”,系統不爲它分配獨立的inode和文件。所以,硬鏈接文件與原始文件其實是同一個文件,只不過是不同的名字而已。我們每添加一個硬鏈接,該文件的inode鏈接數就會增加1;而且只有當該文件的inode連接數爲0時,纔算徹底將它刪除。換言之,由於硬鏈接實際上是指向原文件的inode的指針,因此即便原始文件被刪除,依然可以通過硬鏈接文件來訪問。
1.硬鏈接,以文件副本的形式存在。但不佔用實際空間。
2.不允許給目錄創建硬鏈接
3.硬鏈接只有在同一個文件系統中才能創建

建立硬鏈接:ln a b
[root@CentOS6 project]# echo "Hello world" > readme.txt
[root@CentOS6 project]# ln readme.txt readit.txt	#創建一個硬鏈接
[root@CentOS6 project]# cat readme.txt
Hello world
[root@CentOS6 project]# cat readit.txt
Hello world
[root@CentOS6 project]# ls -l readme.txt
-rw-r--r--. 2 root root 12 Mar 16 23:41 readme.txt
[root@CentOS6 project]# rm -rf readme.txt	#刪除原文件
[root@CentOS6 project]# cat readit.txt	#刪除原文件之後readit.txt依舊可以訪問
Hello world
[root@CentOS6 project]# ls -l readit.txt
-rw-r--r--. 1 root root 12 Mar 16 23:41 readit.txt

在這裏插入圖片描述

ln命令

ln 命令用於創建鏈接文件,格式爲“ln [選項] 目標”,其可用的參數以及作用可以通過ln --help查看:

[root@CentOS6 home]# ln --help
      --backup[=CONTROL]      make a backup of each existing destination file
  -b                          like --backup but does not accept an argument
  -d, -F, --directory         allow the superuser to attempt to hard link
                                directories (note: will probably fail due to
                                system restrictions, even for the superuser)
  -f, --force                 remove existing destination files
  -i, --interactive           prompt whether to remove destinations
  -L, --logical               make hard links to symbolic link references
  -n, --no-dereference        treat destination that is a symlink to a
                                directory as if it were a normal file
  -P, --physical              make hard links directly to symbolic links
  -s, --symbolic              make symbolic links instead of hard links
  -S, --suffix=SUFFIX         override the usual backup suffix
  -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create
                                the links
  -T, --no-target-directory   treat LINK_NAME as a normal file
  -v, --verbose               print name of each linked file
      --help     display this help and exit
      --version  output version information and exit

-b 刪除,覆蓋以前建立的鏈接
-d 允許超級用戶製作目錄的硬鏈接
-f 強制執行
-i 交互模式,文件存在則提示用戶是否覆蓋
-n 把符號鏈接視爲一般目錄
-s 軟鏈接(符號鏈接)
-v 顯示詳細的處理過程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章