1. 程式人生 > >ln 軟體連結目錄時,當連結檔案已經存在時,結果非預期

ln 軟體連結目錄時,當連結檔案已經存在時,結果非預期

建立軟體連結的語法如下:

link -s  target  linkName

 

例項:

ln -s ssh_connect_home link1

 

但是當我們想把此連結指向第二個檔案時,結果卻不如意料那般:

 

仔細一看:

原來這個命令將軟體連結建立到目錄中去了,這是為何?

 

參考一下man:

SYNOPSIS

       ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)

       ln [OPTION]... TARGET                  (2nd form)

       ln [OPTION]... TARGET... DIRECTORY     (3rd form)

       ln [OPTION]... -t DIRECTORY TARGET...  (4th form)

 

DESCRIPTION

       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.

 

原來,當連結檔案已經存在時,被解析成了第三種格式,會在資料夾中建立到每個target的連結(連結名使用檔名)。

解決方法:

使用-n選項。

-n, --no-dereference

              treat LINK_NAME as a normal file if it is a symbolic link to a directory

 

 

 

 

如果target是檔案,則並不存在這種問題: