Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symlink broken right after creation

I downloaded the linux Tor Browser package, which is a self-contained folder. I made a symlink to the run script:

$ 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:

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:

$ 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).

...any ideas?

like image 495
joshlf Avatar asked Jul 19 '13 02:07

joshlf


People also ask

Why are my symbolic links broken?

A symlink is broken (or left dangling) when the file at which it points is deleted or moved to another location. If an application's uninstallation routine doesn't work properly, or is interrupted before it completes, you might be left with broken symlinks.

How do I fix a broken symlink?

The only way to fix these broken symlinks is by deleting them. Your system contains hundreds of dangling links and no one has the time to check for these links manually. In such cases, Linux tools and commands prove to be really helpful.

How do you check if a symlink is broken?

3. Finding Broken Symlinks. The -H, -L and -P options control how symbolic links are treated, and when omitted, use -P as the default. When -P is used and find examines or prints information from a symbolic link, the details are taken from the properties of the symbolic link itself.

What is a dangling symbolic link?

Symbolic links may contain .. path components, which (if used at the start of the link) refer to the parent directories of that in which the link resides. A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent one; the latter case is known as a dangling link.


2 Answers

It's important to know that

ln -s SOURCE TARGET 

create a symlink called TARGET which is symbolically linked to the string SOURCE. 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.

So when you do this:

$ ln -s torbrowser/start-tor-browser ~/bin/torbrowser 

what you are doing is, roughly:

  1. create a directory entry inside your bin subdirectory with name torbrowser.
  2. Make that new directory entry a symbolic link (symlink) to the (relative) path 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:

$ 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 its version string, I can also move the entire folder elsewhere, without breaking the symlink:

$ 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.

like image 136
rici Avatar answered Sep 25 '22 08:09

rici


It is important that the TARGET you specify in

ln -s TARGET LINK_NAME 

is full path of the file/directory. I had this issue, in my case when I cd into target's directory and did

ln -s ./eclipse.ini ~/Desktop/eclipse1 resulted in broken link

enter image description here

But when I did this ln -s $(pwd)/eclipse.ini ~/Desktop/eclipse It worked!

enter image description here

like image 20
Adil Saju Avatar answered Sep 24 '22 08:09

Adil Saju