AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 389997
Accepted
Jérôme Verstrynge
Jérôme Verstrynge
Asked: 2012-05-18 04:39:18 +0800 CST2012-05-18 04:39:18 +0800 CST 2012-05-18 04:39:18 +0800 CST

使用绝对路径和相对路径时,符号链接未按预期更新

  • 772

我正在尝试使用符号链接。我做了一些阅读,发现了以下命令:

Creation -> ln -s {/path/to/file-name} {link-name}
Update -> ln -sfn {/path/to/file-name} {link-name}
Deletion -> rm {link-name}

创建和删除工作正常。但是更新不起作用。执行此命令后,符号链接将失效。

我在这里和那里读到不可能更新/覆盖符号链接。所以网上出现了相互矛盾的信息。谁是对的?如果可以更新/覆盖符号链接,我该如何实现?

更新

这是我的目录结构:

~/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

atest_link已创建,我可以创建,ls -l但它似乎已损坏(与我上面在问题中所说的相反)。

如何执行多目录级链接?

symbolic-link update
  • 4 4 个回答
  • 331972 Views

4 个回答

  • Voted
  1. Sirch
    2012-05-18T04:49:48+08:002012-05-18T04:49:48+08:00

    使用-fwithln将覆盖任何已经存在的链接,因此只要您拥有正确的权限,它就应该可以工作……它一直对我有用。您使用什么操作系统?

    • 179
  2. Best Answer
    Jérôme Verstrynge
    2012-05-18T05:32:38+08:002012-05-18T05:32:38+08:00

    好的,我发现我的错误在哪里:不应该把第一个/放在路径中。

    换句话说,我的问题中的命令应该是:

    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}
    

    考虑到我的情况。

    • 153
  3. Keith Reynolds
    2016-06-09T14:58:01+08:002016-06-09T14:58:01+08:00

    首要问题:

    引用你的话:

    创建和删除工作正常。但是更新不起作用。执行此命令后,符号链接将失效。

    给定目录结构的问题:

    ~/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

    第二期:

    引用你的话:

    我在这里和那里读到不可能更新/覆盖符号链接。所以网上出现了相互矛盾的信息。谁是对的?如果可以更新/覆盖符号链接,我该如何实现?

    要回答你的问题“谁是对的”,我不确定你到底读了什么,或者是如何理解的。但是,以下内容应该有助于澄清:

    1. 可以更新什么,以及
    2. 不使用适当的开关就无法更新什么。


    使用不是目录的目标更新符号链接。

    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
    

    但是,如您所见,如果绝对路径在ln的参数中,它将给出绝对路径。当当前工作目录与链接的父目录不同时,需要提供完整路径。


    相对路径:

    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
    

    但是,如果目标是目录,则更新指向目录的链接将不起作用。

    例子:

    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
    

    如您所见,尽管使用了ln上面 的参数中给出的绝对路径名而没有 -r 选项,符号链接仍然是相对于链接的。


    更新目录链接:

    ln -sfrn:
    如果 LINK_NAME 是目录的符号链接,则 -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
    
    • 15
  4. jirib
    2012-05-18T04:42:30+08:002012-05-18T04:42:30+08:00
    $ 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
    
    • 8

相关问题

  • 有没有常见的符号链接陷阱?

  • Windows 有符号链接吗?

  • 在 Windows 中查找和删除坏的符号链接

  • 在我的服务器上创建符号链接时出现问题:没有这样的文件或目录

  • 如何在 Windows 中创建符号链接?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve