双重使用“ln -s sourceDirA DestanationDirB”时出错的原因

时间:2016-02-24 06:04:35

标签: shell

1.我第一次使用“ln -s workspace workspace2”时 一切都好吗

drwxr-xr-x   2 root root 4096 2月  24 13:49 workspace
lrwxrwxrwx   1 root root    9 2月  24 13:38 workspace2 -> workspace

2.当我双链接时,使用“ln -s workspace workspace2”tiwce,   然后我“cd workspace”,发现工作区在工作区

[root@xxxxxxxx workspace]#ls -al

drwxr-xr-x  2 root root 4096 2月  24 13:49 .
dr-xr-x---. 6 root root 4096 2月  24 13:38 ..
-rw-r--r--  1 root root    0 2月  24 13:27 test.php
lrwxrwxrwx  1 root root    9 2月  24 13:49 workspace -> workspace

为什么?

1 个答案:

答案 0 :(得分:0)

因为当ln呈现目标的现有目录时,它会在那里创建具有相同名称的链接。

$ rm -rf work*
$ mkdir workspace
$ ln -s workspace workspace/
$ ls -l workspace/
total 0
lrwxrwxrwx 1 td td 9 Feb 23 23:16 workspace -> workspace

手册页说

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.

在您的第一种情况下,workspace2不存在,因此您就是表单1.

$ mkdir workspace
$ ln -s workspace workspace2
$ ls -l
total 4
drwxr-xr-x 2 td td 4096 Feb 23 23:24 workspace
lrwxrwxrwx 1 td td    9 Feb 23 23:24 workspace2 -> workspace
$ ls -l workspace
total 0

在第二种情况下,workspace2是指向目录的链接,因此您就是表单3。

$ ln -s workspace workspace2
$ ls -l
total 4
drwxr-xr-x 2 td td 4096 Feb 23 23:25 workspace
lrwxrwxrwx 1 td td    9 Feb 23 23:24 workspace2 -> workspace
$ ls -l workspace
total 0
lrwxrwxrwx 1 td td 9 Feb 23 23:25 workspace -> workspace