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 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'

选项参数说明

-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错误,连接文件或文件夹不能用

posted @ 2016-06-29 18:22  学着总结,学会总结  阅读(284)  评论(0编辑  收藏  举报