天天看點

Linux指令詳解----ln

ln指令

ln指令為檔案或檔案夾建立連接配接,連接配接類型有硬連結和符号連接配接兩種,符号連接配接需要使用“-s”選項

ln文法

ln [選項] 參數
           

使用 ln --help檢視可用選項

[root@node1 ~]# ln --help
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.
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               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

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:

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

  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

Report ln bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'ln invocation'
           

選項參數說明

-b或--backup:删除,覆寫目标檔案之前的備份;
 -d或-F或——directory:建立目錄的硬連接配接; 
-f或——force:強行建立檔案或目錄的連接配接,不論檔案或目錄是否存在; 
-i或——interactive:覆寫既有檔案之前先詢問使用者; 
-n或--no-dereference:把符号連接配接的目的目錄視為一般檔案; 
-s或——symbolic:對源檔案建立符号連接配接,而非硬連接配接; 
-S<字尾備份字元串>或--suffix=<字尾備份字元串>:用"-b"參數備份目标檔案後,備份檔案的字尾會被加上一個備份字元串,預設的備份字元串是符号“~”,使用者可通過“-S”參數來改變它; 
-v或——verbose:顯示指令執行過程; 
-V<備份方式>或--version-control=<備份方式>:用“-b”參數備份目标檔案後,備份檔案的字尾會被加上一個備份字元串,這個字元串不僅可用“-S”參數變更,當使用“-V”參數<備份方式>指定不同備份方式時,也會産生不同字尾的備份字元串; 
--help:線上幫助; 
--version:顯示版本資訊。
           

參數

  1. 源檔案:指定連接配接的源檔案。如果使用-s選項建立符号連接配接,則“源檔案”可以是檔案或者目錄。建立硬連接配接時,則“源檔案”參數隻能是檔案; 目标檔案:指定源檔案的目标連接配接檔案。
  2. 目标檔案:指定源檔案的目标連接配接檔案

執行個體

先使用硬連結連接配接一個檔案夾實驗一下效果

[root@node1 data]# pwd
/data
[root@node1 data]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jun 27 02:54 test
drwxr-xr-x. 3 1001 root 4096 Jun 26 18:34 webbench-1.5
-rw-r--r--. 1 root root 7675 May 19  2009 webbench-1.5.tar.gz
#/data 目錄下有兩個目錄一個檔案,就在此基礎上進行操作檢視效果
[root@node1 data]# ln /data/webbench-1.5 /data/test
ln: `webbench-1.5': hard link not allowed for directory
[root@node1 data]# ln /data/webbench-1.5 /data/test/
ln: `webbench-1.5': hard link not allowed for directory
[root@node1 data]# ln /data/webbench-1.5/ /data/test/
ln: `webbench-1.5/': hard link not allowed for directory
#怎麼操作檔案夾是不能連接配接的
[root@node1 data]# ln /data/webbench-1.5.tar.gz /data/test
[root@node1 data]# ls
test  webbench-1.5  webbench-1.5.tar.gz
[root@node1 data]# cd /data/test/
[root@node1 test]# ls
webbench-1.5.tar.gz
[root@node1 test]# ls -l
total 8
-rw-r--r--. 2 root root 7675 May 19  2009 webbench-1.5.tar.gz
#硬連結連接配接檔案操作成功,相當複制檔案到指定目錄
           

下邊看軟體連操作

[root@node1 data]# ln -s /data/webbench-1.5 /data/test/
[root@node1 data]# ls test
webbench-1.5  webbench-1.5.tar.gz
[root@node1 data]# ln -s /data/webbench-1.5 /data/test/webbench-bak
[root@node1 data]# ls test
webbench-1.5  webbench-1.5.tar.gz  webbench-bak
[root@node1 data]# mkdir test/webbench-bak-2
[root@node1 data]# ln -s /data/webbench-1.5 /data/test/webbench-bak-2
[root@node1 data]# ls test/
webbench-1.5  webbench-1.5.tar.gz  webbench-bak  webbench-bak-2
[root@node1 data]# ls test/webbench-bak-2/
webbench-1.5
#如果目标檔案夾已存在,會把目前檔案夾連接配接到目标檔案夾下生成和源檔案夾同名的檔案夾
#如果目标檔案夾不存在,直接連接配接源檔案到目标檔案夾,同時生成目标檔案夾
[root@node1 test]# ll
total 12
lrwxrwxrwx. 1 root root   18 Jun 27 03:22 webbench-1.5 -> /data/webbench-1.5
-rw-r--r--. 2 root root 7675 May 19  2009 webbench-1.5.tar.gz
lrwxrwxrwx. 1 root root   18 Jun 27 03:23 webbench-bak -> /data/webbench-1.5
drwxr-xr-x. 2 root root 4096 Jun 27 03:23 webbench-bak-2
           

備注

以上資訊本人操作實驗資料,操作過程強自己記憶,想檢視更過linux指令,請到http://man.linuxde.net/檢視

在進行連接配接的時候一定要使用全路徑,否則會出現Too many levels of symbolic links錯誤,連接配接檔案或檔案夾不能用

繼續閱讀