bash 创建后立即断开符号链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17737065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symlink broken right after creation
提问by joshlf
I downloaded the linux Tor Browser package, which is a self-contained folder. I made a symlink to the run script:
我下载了 linux Tor Browser 包,它是一个独立的文件夹。我做了一个指向运行脚本的符号链接:
$ ln -s torbrowser/start-tor-browser ~/bin/torbrowser
However, the link was broken upon creation. All I did was run that command, nothing else, and it was broken. I did ls and got:
但是,该链接在创建时已断开。我所做的只是运行该命令,没有别的,它被破坏了。我做了 ls 并得到:
lrwxrwxrwx 1 synful synful 28 Jul 18 21:52 torbrowser -> torbrowser/start-tor-browser
...which is weird because torbrowser/start-tor-browser had 755 permissions. Also, I ran file
:
...这很奇怪,因为 torbrowser/start-tor-browser 有 755 个权限。另外,我跑了file
:
$ file ~/bin/torbrowser
bin/torbrowser: broken symbolic link to `torbrowser/start-tor-browser'
I made a new bash script and a symlink to it to test this, and had no such problems. I'm not sure why it's only happening with start-tor-browser. It's got normal permissions and is just a normal bash script (even according to the file command).
我制作了一个新的 bash 脚本和一个符号链接来测试它,并且没有出现这样的问题。我不确定为什么它只发生在 start-tor-browser 上。它具有普通权限,只是一个普通的 bash 脚本(即使根据 file 命令)。
...any ideas?
...有任何想法吗?
回答by rici
It's important to know that
重要的是要知道
ln -s SOURCE TARGET
create a symlink called TARGET which is symbolically linked to the stringSOURCE
. If SOURCE
is a relative path (that is, it does not start with /
), then it is interpreted relative to the directory that TARGET
is in. If it is an absolute path, then it's an absolute path. If it is a string which could not be a path, or includes a non-existing path or file, or is otherwise not a valid path string, no matter. ln -s
does not check that SOURCE exists or is even a valid path. You could store almost any shortish string you wanted in the dirent.
创建一个名为 TARGET 的符号链接,它以符号方式链接到字符串SOURCE
。如果SOURCE
是相对路径(即不以 开头/
),则相对于所在目录进行解释TARGET
。如果是绝对路径,则是绝对路径。如果它是一个不能是路径的字符串,或者包含一个不存在的路径或文件,或者不是一个有效的路径字符串,无论如何。ln -s
不检查 SOURCE 是否存在或什至是有效路径。你可以在目录中存储几乎任何你想要的短字符串。
So when you do this:
所以当你这样做时:
$ ln -s torbrowser/start-tor-browser ~/bin/torbrowser
what you are doing is, roughly:
你在做什么,大致是:
- create a directory entry inside your
bin
subdirectory with nametorbrowser
. - Make that new directory entry a symbolic link (symlink) to the (relative) path
torbrowser/start-tor-browser
- 在您的
bin
子目录中创建一个名为 name的目录条目torbrowser
。 - 使该新目录条目成为(相对)路径的符号链接(symlink)
torbrowser/start-tor-browser
The new symlink is a circular. ~/bin/torbrowser
is linked to ~/bin/torbrowser/start-tor-browser
, which means you have to follow the symlink in order to resolve the symlink. If you try to use it, you'll see:
新的符号链接是一个圆形。~/bin/torbrowser
链接到~/bin/torbrowser/start-tor-browser
,这意味着您必须遵循符号链接才能解析符号链接。如果您尝试使用它,您会看到:
$ cat ~/bin/torbrowser
cat: /home/joshlf13/bin/torbrowser: Too many levels of symbolic links
$
Sometimes -- often, even -- the ability to symlink to a relative path is extremely handy. A common use is getting rid of version numbers:
有时 - 通常,甚至 - 符号链接到相对路径的能力非常方便。一个常见的用途是摆脱版本号:
$ ln -s apps/my_fancy_app_v2.63.1 apps/my_fancy_app
Now, not only can I call my_fancy_app without remembering it's version string, I can also move the entire folder elsewhere, without breaking the symlink:
现在,我不仅可以在不记住版本字符串的情况下调用 my_fancy_app,还可以将整个文件夹移动到其他地方,而不会破坏符号链接:
$ mv apps /usr/local/apps
But other times -- as in your example, I think -- you need to symlink to an absolute path.
但其他时候——就像在你的例子中一样,我认为——你需要符号链接到绝对路径。
As for the permissions, symlinks always have permissions lrwxrwxrwx
because the actual permissions used by file operations are the permissions on the real file. (You can think of that as meaning that anyone can follow the symlink, but that's not quite true: they'd also need read permissions for any directory they need to follow. More accurately, anyone who can see the symlink can see the name it points to, even if they have no access to the file with that name.
至于权限,符号链接总是有权限的,lrwxrwxrwx
因为文件操作使用的实际权限是对真实文件的权限。(您可以认为这意味着任何人都可以关注符号链接,但这并不完全正确:他们还需要对需要关注的任何目录具有读取权限。更准确地说,任何可以看到符号链接的人都可以看到它的名称指向,即使他们无权访问具有该名称的文件。
回答by Adil Saju
It is important that the TARGET
you specify in
重要的是TARGET
您在
ln -s TARGET LINK_NAME
is full pathof the file/directory.
I had this issue, in my case when I cd
into target's directory and did
是文件/目录的完整路径。我遇到了这个问题,就我而言,当我cd
进入目标目录并执行
ln -s ./eclipse.ini ~/Desktop/eclipse1
resulted in broken link
ln -s ./eclipse.ini ~/Desktop/eclipse1
导致链接断开
But when I did this ln -s $(pwd)/eclipse.ini ~/Desktop/eclipse
It worked!
但是当我这样做时ln -s $(pwd)/eclipse.ini ~/Desktop/eclipse
它起作用了!
回答by alinux
the above usage given for ln:
为 ln 给出的上述用法:
ln -s SOURCE TARGET
ln -s 源目标
is correct, but confusing when referred to the man page:
是正确的,但在参考手册页时令人困惑:
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... [-T] TARGET LINK_NAME(第一种形式)
as 'TARGET' has different meaning
因为“目标”有不同的含义
回答by ozgeneral
Note: this can also happen due to permissions eg. if you try
注意:这也可能由于权限而发生,例如。如果你试试
sudo ln -s /home/somesuperuser/commonfile /home/somenormaluser/commonfile
sudo ln -s /home/somesuperuser/commonfile /home/somenormaluser/commonfile
this would not work, while
这行不通,而
sudo mv /home/somesuperuser/commonfile /usr/share/commonfile
sudo ln -s /usr/share/commonfile /home/somenormaluser/commonfile
sudo ln -s /usr/share/commonfile /home/somesuperuser/commonfile
does work
确实有效