一天一個 Linux 命令(18):ln 命令


一天一個 Linux 命令(18):ln 命令

一、簡介

Linux里面的ln(英文全拼:link files)命令是一個非常重要命令,它的功能是為某一個文件在另外一個位置建立一個同步的鏈接。

當需要在不同的目錄,用到相同的文件時,不需要在每一個需要的目錄下都放一個必須相同的文件,只要在某個固定的目錄,放上該文件,然后在 其它的目錄下用ln命令鏈接(link)它就可以,不必重復的占用磁盤空間。

二、格式說明

ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
  or:  ln [OPTION]... TARGET                  (2nd form)
  or:  ln [OPTION]... TARGET... DIRECTORY     (3rd form)
  or:  ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
 
ln [參數][源文件或目錄][目標文件或目錄]
 

Usage: ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
  or:  ln [OPTION]... TARGET                  (2nd form)
  or:  ln [OPTION]... TARGET... DIRECTORY     (3rd form)
  or:  ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.

Mandatory arguments to long options are mandatory for short options too.
      --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               dereference TARGETs that are symbolic links
  -n, --no-dereference        treat LINK_NAME as a normal file if
                                it is a symbolic link to a directory
  -P, --physical              make hard links directly to symbolic links
  -r, --relative              create symbolic links relative to link location
  -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 always
  -v, --verbose               print name of each linked file
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

Using -s ignores -L and -P.  Otherwise, the last option specified controls
behavior when a TARGET is a symbolic link, defaulting to -P.

三、選項說明

通過find --help 或者man find 進行詳細查看

-b 刪除,覆蓋以前建立的鏈接

-d 允許超級用戶制作目錄的硬鏈接

-f 強制執行

-i 交互模式,文件存在則提示用戶是否覆蓋

-n 把符號鏈接視為一般目錄

-s 軟鏈接(符號鏈接)

-v 顯示詳細的處理過程

-S “-S<字尾備份字符串> ”或 “--suffix=<字尾備份字符串>”

-V “-V<備份方式>”或“--version-control=<備份方式>”

--help 顯示幫助信息

--version 顯示版本信息

四、命令功能

Linux文件系統中的鏈接(link),我們可以將其視為文件或者目錄的別名,而鏈接又可分為兩種 : 硬鏈接(hard link)與軟鏈接(symbolic link),硬鏈接的意思是一個檔案可以有多個名稱,而軟鏈接的方式則是產生一個特殊的文件或者目錄,該檔文件或者目錄指向另一個文件或者目錄的位置。硬鏈接是存在同一個文件系統中,而軟鏈接卻可以跨越不同的文件系統。

五、軟鏈接和硬鏈接區別

1.軟鏈接

(1).軟鏈接,以路徑的形式存在。類似於Windows操作系統中的快捷方式

(2).軟鏈接可以跨文件系統 ,硬鏈接不可以

(3).軟鏈接可以對一個不存在的文件名進行鏈接

(4).軟鏈接可以對目錄進行鏈接

2.硬鏈接

(1).硬鏈接,以文件副本的形式存在。但不占用實際空間。

(2).不允許給目錄創建硬鏈接

(3).硬鏈接只有在同一個文件系統中才能創建

這里有兩點要注意:

1.ln命令會保持每一處鏈接文件的同步性,也就是說,不論你改動了哪一處,其它的文件都會發生相同的變化;

2.ln的鏈接又分軟鏈接和硬鏈接兩種,軟鏈接就是ln –s 源文件 目標文件,它只會在你選定的位置上生成一個文件的鏡像,不會占用磁盤空間,硬鏈接 ln 源文件 目標文件,沒有參數-s, 它會在你選定的位置上生成一個和源文件大小相同的文件,無論是軟鏈接還是硬鏈接,文件都保持同步變化。

ln指令用在鏈接文件或目錄,如同時指定兩個以上的文件或目錄,且最后的目的地是一個已經存在的目錄,則會把前面指定的所有文件或目錄復制到該目錄中。若同時指定多個文件或目錄,且最后的目的地並非是一個已存在的目錄,則會出現錯誤信息

六、常見用法

1.給文件創建軟鏈接,為test.txt文件創建軟鏈接link_test.txt,如果test.txt丟失,link_test.txt將失效:

ln -s test.txt link_test.txt

2.給文件創建硬鏈接,為test.txt創建硬鏈接hard_link_test.txt,test.txt與hard_link_test.txt的各項屬性相同

ln test.txt hard_link_test.txt

結果如下:

# ll -a|grep test.txt
-rwxrwxrwx   2 root root    0 Sep  8 18:17 hard_link_test.txt
lrwxrwxrwx   1 root root    8 Sep 22 17:30 link_test.txt -> test.txt
-rwxrwxrwx   2 root root    0 Sep  8 18:17 test.txt

3.接上面兩個例子,鏈接完畢后,刪除和重建鏈接原文件

依次執行看下效果:

rm -rf test.txt

ll -a|grep test.txt

touch test.txt

ll -a|grep test.txt

echo "hello" > test.txt

ll -a|grep test.txt

說明:

(1)源文件被刪除后,並沒有影響硬鏈接文件;軟鏈接文件在Linux系統下不斷的閃爍,提示源文件已經不存在

(2)重建源文件后,軟鏈接不在閃爍提示,說明已經鏈接成功,找到了鏈接文件系統;重建后,硬鏈接文件並沒有受到源文件影響,硬鏈接文件的內容還是保留了刪除前源文件的內容,說明硬鏈接已經失效

4.將文件鏈接為另一個目錄中的相同名字

在test2目錄中創建了test.txt的硬鏈接,修改test2目錄中的test.txt文件,同時也會同步到源文件

ln test.txt test2/

5.給目錄創建軟鏈接

ln -sv /root/test/test3 /root/test/test5
# ll|grep test3
drwxr-xr-x 3 root root 4096 Sep  8 18:23 test3
lrwxrwxrwx 1 root root   16 Sep 22 17:47 test5 -> /root/test/test3

說明:

(1)目錄只能創建軟鏈接

(2)目錄創建鏈接必須用絕對路徑,相對路徑創建會不成功,會提示:符號連接的層數過多 這樣的錯誤

(3)在鏈接目標目錄中修改文件都會在源文件目錄中同步變化

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM