LN

名字

ln - make links between files

梗概

  1. ln [OPTION]... [-T] TARGET LINK_NAME
  2. ln [OPTION]... TARGET
  3. ln [OPTION]... TARGET... DIRECTORY
  4. ln [OPTION]... -t DIRECTORY TARGET...

描述

In the 1st form, create a link to TARGET with the name LINK_NAME

In the 2and form, create a link to TARGET in the current directory.

In the 3rd and 4th forms, create links to each TARGET in DIRECOTRY.

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
  • -v, --verbose print name of each lined file
  • --help display this help and exit
  • -s, --symbolic
  • --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

ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。当我们需要在不同的目录,用到相同的文件时,我们不需要在每个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下用ln命令链接它就可以,不必重复的占用磁盘空间。

命令功能:

Linux文件系统中,有所谓的链接(link),我们可以将其视为档案的别名,而链接又可分为两种:硬链接(hard link)与软链接(symbolic link),硬链接的意思是一个档案可以有多个名称,而软链接的方式则是产生一个特殊的档案,该档案的内容是指向另一个档案的位置。硬链接是存在同一个文件系统中,而软链接去可以跨越不同的文件系统。

软链接

  • 软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
  • 软链接可以跨文件系统,硬链接不可以
  • 软链接可以对一个不存在的文件名进行链接
  • 软链接可以对目录进行链接

硬链接

  • 以文件复本的形式存在。但不占用实际空间。
  • 不允许给目录创建硬链接
  • 硬链接只有在同一个文件系统中才能创建

注意点

  • ln命令会保持每一处链接文件的同步性,也就是说,不论你发动了哪一处,其它的文件都会发生相同的变化
  • ln的链接又分软链接和硬链接两种,软链接就是ln -s 源文件 目标文件,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间,硬链接 ln 源文件 目标文件 它会在你选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。

使用实例:

给文件创建软链接 为log2013.log文件创建软链接link2013,如果log2013.log丢失,link2013将失效

$ ls
log2013.log
$ ls
log2013.log
$ ln -s log2013.log link2013
$ ls
link2013  log2013.log
$

给文件创建硬链接 为log2013.log创建硬链接ln2013, log2013.log与ln2013的各项属性相同

$ln log2013.log ln2013
$ll
总用量 8
lrwxrwxrwx. 1 jianglei jianglei 11 9月  23 05:47 link2013 -> log2013.log
-rw-rw-r--. 2 jianglei jianglei 70 9月  23 05:46 ln2013
-rw-rw-r--. 2 jianglei jianglei 70 9月  23 05:46 log2013.log

接上面两实例,链接完毕后,删除和重建链接原文件 源文件被删除后,并没有影响到硬链接文件;软链接文件在centos系统下不断的闪烁,提示源文件已经不存在

重建源文件后,软链接不在闪烁提示,说明已经链接成功,找到了链接文件系统;重建后,硬链接文件并没有受到源文件影响,硬链接文件的内容还是保留了删除前源文件的内容,说明硬链接已经失效

$rm -rf log2013.log
$touch log2013.log
$cat >> log2013.log
2013-01
2013-02
2013-03
^C
$cat link2013
2013-01
2013-02
2013-03
$cat ln2013
this is the first line
this is the second line
this is the third line

给目录创建软链接 目录只能创建软链接 目录创建链接必须用绝对路径,相对路径创建会不成功,会提示:符号连接的层数过多 这样的错误 在链接目标目录中修改文件都会在源文件目录中同步变化

给目录创建软链接实例

$ sudo ln -s spring-2.0.0.RELEASE  spring-cli2 
$ ls -al | grep spring-cli2
lrwxr-xr-x   1 root      wheel       20  4 20 13:29 spring-cli2 -> spring-2.0.0.RELEASE

results matching ""

    No results matching ""