为什么我的符号链接不起作用?

oro*_*aki 29 linux bash symbolic-link ubuntu

我正在努力更好地理解符号链接......并且运气不佳。这是我更改了用户名/主机的实际 shell 输出:

username@host:~$ mkdir actual
username@host:~$ mkdir proper
username@host:~$ touch actual/file-1.txt
username@host:~$ echo "file 1" > actual/file-1.txt
username@host:~$ touch actual/file-2.txt
username@host:~$ echo "file 2" > actual/file-2.txt
username@host:~$ ln -s actual/file-1.txt actual/file-2.txt proper
username@host:~$ # Now, try to use the files through their links
username@host:~$ cat proper/file-1.txt
cat: proper/file-1.txt: No such file or directory
username@host:~$ cat proper/file-2.txt
cat: proper/file-2.txt: No such file or directory
username@host:~$ # Check that actual files do in fact exist
username@host:~$ cat actual/file-1.txt
file 1
username@host:~$ cat actual/file-2.txt
file 2
username@host:~$ # Remove the links and go home :(
username@host:~$ rm proper/file-1.txt
username@host:~$ rm proper/file-2.txt
Run Code Online (Sandbox Code Playgroud)

我认为符号链接应该透明地操作,从某种意义上说,您可以对它指向的文件进行操作,就好像您直接访问该文件一样(当然,rm在链接被简单删除的情况下当然除外))。

ner*_*ler 62

符号链接往往喜欢完整路径或相对于链接,否则它们通常会在file-1.txt本地寻找(奇怪的是)。

导航到proper并执行ls -l,您可以看到符号链接正在寻找actual/file-1.txt,什么时候应该找到../actual/file-1.txt

所以你有两个选择:

  1. 给出完整路径

    ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
    
    Run Code Online (Sandbox Code Playgroud)
  2. 导航到您希望链接所在的文件夹并从那里链接

    cd proper
    ln -s ../actual/file-1.txt ../actual/file-2.txt ./
    
    Run Code Online (Sandbox Code Playgroud)

编辑:保存打字的提示。

你可以做 ln -s ~/actual/file-{1,2}.txt ~/proper

大括号中的项目被替换并放在一起,创建命令

ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
Run Code Online (Sandbox Code Playgroud)

它将两个文件链接到目标目录。随着您在 shell 中的进一步深入,可以节省一些主要的输入。

  • 谢谢 - 在我读到你的答案之前,我正要拔头发。 (3认同)