Jér*_*nge 232 symbolic-link update
我正在尝试使用符号链接。我做了一些阅读,发现以下命令:
Creation -> ln -s {/path/to/file-name} {link-name}
Update -> ln -sfn {/path/to/file-name} {link-name}
Deletion -> rm {link-name}
Run Code Online (Sandbox Code Playgroud)
创建和删除工作正常。但是更新不起作用。执行此命令后,符号链接无效。
我在这里和那里都读过,无法更新/覆盖符号链接。所以网络上有矛盾的信息。谁是对的?如果可以更新/覆盖符号链接,我该如何实现?
更新
这是我的目录结构:
~/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
Run Code Online (Sandbox Code Playgroud)
从~/scripts/test/
,当我执行:
ln -s /remote_loc/site1 test_link
Run Code Online (Sandbox Code Playgroud)
atest_link
已创建,我可以创建,ls -l
但它似乎已损坏(与我上面在问题中所说的相反)。
如何执行多目录级链接?
Sir*_*rch 179
使用-f
withln
会覆盖任何已经存在的链接,所以只要你有正确的权限,它应该可以工作......它总是对我有用。您使用什么操作系统?
Jér*_*nge 153
好的,我找到了我的错误所在:不应将第一个/
放在路径中。
换句话说,我的问题中的命令应该是:
Creation -> ln -s {path/to/file-name} {link-name}
Update -> ln -sfn {path/to/file-name} {link-name}
Run Code Online (Sandbox Code Playgroud)
代替
Creation -> ln -s {/path/to/file-name} {link-name}
Update -> ln -sfn {/path/to/file-name} {link-name}
Run Code Online (Sandbox Code Playgroud)
考虑到我的情况。
Kei*_*lds 14
引用你:
创建和删除工作正常。但是更新不起作用。执行此命令后,符号链接无效。
给定目录结构的问题:
~/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 或当前工作目录中创建了一个符号链接,该链接指向 /remote_loc/site1 中 / 或根目录之外的一个不存在的文件
如果您的 PWD 在 ~/scripts/ 中,那么您应该使用它:
ln -s remote_loc/site1 test_link
否则你可以使用完整的绝对路径,如:
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
Run Code Online (Sandbox Code Playgroud)
但是,如您所见,如果绝对路径在 inln
的参数中,它将给出绝对路径。当前工作目录与链接的父目录不同时,需要提供完整路径。
相对路径:
ln -sfr:
-r 或--relative创建相对于链接位置的符号链接。
例子:
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
Run Code Online (Sandbox Code Playgroud)
但是,如果目标是目录,则更新指向目录的链接将不起作用。
例子:
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
Run Code Online (Sandbox Code Playgroud)
如您所见,尽管使用了ln
上面 's 参数中给出的绝对路径名而没有 -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
Run Code Online (Sandbox Code Playgroud)
相比之下:
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
Run Code Online (Sandbox Code Playgroud)
$ 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
Run Code Online (Sandbox Code Playgroud)