Linux命令詳解----ln

ln命令

ln命令爲文件或文件夾建立鏈接,鏈接類型有硬連接和符號鏈接兩種,符號鏈接須要使用「-s」選項node

ln語法

ln [選項] 參數

使用 ln --help查看可用選項linux

[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 bug-coreutils@gnu.org
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'

選項參數說明web

-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/查看less

在進行鏈接的時候必定要使用全路徑,不然會出現Too many levels of symbolic links錯誤,鏈接文件或文件夾不能用ide

相關文章
相關標籤/搜索