Too many levels of symbolic links
As Dubu points out in a comment, the issue lies in your relative paths. I had a similar problem symlinking my nginx configuration from /usr/local/etc/nginx
to /etc/nginx
. If you create your symlink like this:
cd /usr/local/etc
ln -s nginx/ /etc/nginx
You will in fact make the link /etc/nginx -> /etc/nginx, because the source path is relative to the link's path. The solution is as simple as using absolute paths:
ln -s /usr/local/etc/nginx /etc/nginx
If you want to use relative paths and have them behave the way you probably expect them to, you can use pwd
to put the current working directory path in, like so:
cd /usr/local/etc
ln -s "$(pwd)/nginx/" /etc/nginx
You would want to have the quotes around the path, to make sure things like spaces in your current path are escaped. Note that you must use double quotes when doing this, as $(pwd)
will not be substituted if you use single quotes.
On the surface, what you've suggested you've tried works for me.
Example
$ mkdir -p test/src test/firefox
$ tree --noreport -fp
.
`-- [drwxrwxr-x] ./test
|-- [drwxrwxr-x] ./test/firefox
`-- [drwxrwxr-x] ./test/src
Make the symbolic link:
$ ln -s test/src test/firefox
$ tree --noreport -fp
.
`-- [drwxrwxr-x] ./test
|-- [drwxrwxr-x] ./test/firefox
| `-- [lrwxrwxrwx] ./test/firefox/src -> test/src
`-- [drwxrwxr-x] ./test/src
Running it a 2nd time would typically produce this:
$ ln -s test/src test/firefox
ln: failed to create symbolic link ‘test/firefox/src’: File exists
So you likely have something else going on here. I would suspect that you have a circular reference where a link is pointing back onto itself.
You can use find
to sleuth this out a bit:
$ cd /suspected/directory
$ find -L ./ -mindepth 15
Symlinks are relative to the parent directory of the link, not of the current directory of the ln
process.
When you do:
cd /top/dir
ln -s test/src test/firefox
(where test/firefox
is a directory), you're making a test/firefox/src
symlink whose target is test/src
.
That test/src
is relative to the test/firefox
directory, so that's a symlink to /top/dir/test/firefox/test/src
.
If you wanted that symlink to be a link to /top/dir/test/src
, you'd need to write:
ln -s ../src test/firefox/
Or
ln -s /top/dir/test/src test/firefox/
though it's generally a bad idea to make symlinks to absolute paths as they are easily broken when directories are renamed or filesystems are mounted elsewhere.
With GNU ln
, you can use its -r
option to let it make the calculation by itself:
$ ln -rs test/src test/firefox/
$ ls -ld test/firefox/src
lrwxrwxrwx 1 chazelas chazelas 6 Nov 29 15:59 test/firefox/src -> ../src