我正在尝试使用符号链接。 我做了一些阅读,并find以下命令:
Creation -> ln -s {/path/to/file-name} {link-name} Update -> ln -sfn {/path/to/file-name} {link-name} Deletion -> rm {link-name}
创build和删除工作正常。 但更新不起作用。 执行此命令后,符号链接将变为无效。
我在这里和那里读到,它是不可能更新/重写符号链接。 所以网上有矛盾的信息。 谁是对的? 如果一个符号链接可以更新/覆盖,我怎么能做到这一点?
更新
这是我的目录结构:
~/scripts/test/ ~/scripts/test/remote_loc/ ~/scripts/test/remote_loc/site1/ ~/scripts/test/remote_loc/site1/stuff1.txt ~/scripts/test/remote_loc/site2/ ~/scripts/test/remote_loc/site2/stuff2.txt ~/scripts/test/remote_loc/site2/ ~/scripts/test/remote_loc/site3/stuff3.txt
从~/scripts/test/
,当我执行:
ln -s /remote_loc/site1 test_link
一个test_link
被创build,并且我可以ls -l
,但它似乎被破坏(与我在上面提到的在我的问题中相反)。
我怎样才能执行一个多目录级别的链接?
在ln
使用-f
将覆盖已经存在的任何链接,只要您拥有正确的权限,就应该可以正常工作。 你在使用什么操作系统?
好的,我发现我的错误是:不应该把第一个/
在path。
换句话说,我的问题中的命令应该是:
Creation -> ln -s {path/to/file-name} {link-name} Update -> ln -sfn {path/to/file-name} {link-name}
代替
Creation -> ln -s {/path/to/file-name} {link-name} Update -> ln -sfn {/path/to/file-name} {link-name}
考虑我的情况。
$ touch test1 test2 $ ln -sf test2 test1 $ ls -l test[12] lrwxrwxrwx 1 user01 user01 5 2012-05-17 14:41 test1 -> test2 -rw-r--r-- 1 user01 user01 0 2012-05-17 14:41 test2
引用你:
创build和删除工作正常。 但更新不起作用。 执行此命令后,符号链接将变为无效。
问题与给定的目录结构:
〜/ scripts / test /〜/ scripts / test / remote_loc /〜/ scripts / test / remote_loc / site1 /〜/ scripts / test / remote_loc / site1 / stuff1.txt〜/ scripts / test / remote_loc / site2 /〜/ scripts /test/remote_loc/site2/stuff2.txt〜/ scripts / test / remote_loc / site2 /〜/ scripts / test / remote_loc / site3 / stuff3.txt
并使用命令:
ln -s /remote_loc/site1 test_link
它是否在您的$ PWD或当前的工作目录中创build了一个符号链接,该目录指向/ remote_loc / site1上的/或root的不存在的文件
如果你的PWD在〜/ scripts /中,那么你应该使用这个:
ln -s remote_loc/site1 test_link
否则你可以使用完整的绝对path,如:
ln -s /home/yourusername/remote_loc/site1 test_link
引用你:
我在这里和那里读到,它是不可能更新/重写符号链接。 所以网上有矛盾的信息。 谁是对的? 如果一个符号链接可以更新/覆盖,我怎么能做到这一点?
要回答你的问题“谁是对的”,我不确定你究竟读了什么,或者怎么理解。 但是,下面应该帮助清理:
更新不是目录的目标的符号链接。
ln -sf:
-f或–force删除现有的目标文件,用于更新链接的目标或目标。
例:
ln -sf /tmp/test /tmp/test.link; ls -go /tmp |grep test -rw-r--r-- 1 0 Jun 8 17:19 test lrwxrwxrwx 1 9 Jun 8 17:27 test.link -> /tmp/test
但是,正如你所看到的那样,如果绝对path是在论据中的话,它将会给予绝对path。 当前工作目录不同于链接的父目录时,需要完整path。
相对path:
ln -sfr:
-r或–relative创build相对于链接位置的符号链接。
例:
ln -sfr /tmp/test /tmp/test.link ; ls -go /tmp| grep test -rw-r--r-- 1 0 Jun 8 17:19 test lrwxrwxrwx 1 4 Jun 8 17:27 test.link -> test
但是,如果目标是目录,则更新到目录的链接将不起作用。
例:
ln -sf /tmp/testdir /tmp/testdir.link ; ls -go /tmp |grep testdir drwxr-xr-x 2 4096 Jun 8 17:48 testdir lrwxrwxrwx 1 7 Jun 8 17:47 testdir.link -> testdir
正如你所看到的,尽pipe使用绝对path名在ln
的参数上面没有-r选项,符号链接仍然是相对于链接。
更新目录链接:
ln -sfrn:
如果是链接到目录的符号链接,则-n或–no-dereference会将LINK_NAME视为普通文件。
例:
ln -sfn /tmp/testdir /tmp/testdir.link; ls -go /tmp| grep testdir drwxr-xr-x 2 4096 Jun 8 17:48 testdir lrwxrwxrwx 1 12 Jun 8 17:48 testdir.link -> /tmp/testdir
与之相反:
ln -sfnr /tmp/testdir /tmp/testdir.link; ls -go /tmp| grep testdir drwxr-xr-x 2 4096 Jun 8 17:48 testdir lrwxrwxrwx 1 7 Jun 8 17:48 testdir.link -> testdir