I have an installed application in /home/appuser/app/
In that directory there is a separate directory confs
for the config files, which is symlinked to ../app-confs
, like so:
lrwxrwxrwx 1 appuser appuser 19 Mar 25 15:22 confs -> ../app-confs/
Looks fine, works fine.
Now I'd like other users to use that application from the same directory (/home/appuser/app/
), so the updates can be done in only one place, but with a different configs directory.
So I create a symlink in /home/user-a/
to /home/appuser/app/
. And I create a new configs directory confs
in /home/user-a/
.
When list /home/user-a/
, I do see
drwxr-xr-x 8 user-a user-a 4096 Apr 29 10:57 app
drwxr-xr-x 8 user-a user-a 4096 Apr 29 10:57 app-confs
and in /home/user-a/app
, I see
...
lrwxrwxrwx 1 appuser appuser 19 Mar 25 15:22 confs -> ../app-confs/
...
But when I list /home/user-a/confs/
, I see files from /home/appuser/confs/
, the original confs and not the relative one.
Is there a way to have a relative symlink with a symlinked directory?
Try this:
mkdir ~/TEST
cd ~/TEST
mkdir global
cd global
mkdir app
mkdir conf
touch conf/global_conf
cd app
ln -s ../conf conf
cd ~/TEST
mkdir user
cd user
ln -s ~/TEST/global/app app
mkdir conf
touch conf/user_conf
cd app
ls -l conf/
You would expect "user_conf" file to be shown, or not?
Best Answer
A: Negative!
Symbolic links are like cd - when you enter into a symlinked directory, you are effectively cding into the target directory. Therefore all relative paths (i.e. symbolic links) in that (and any other) directory will be relative to the physical location of the directory where the symlink is located. In other words...
is effectively
because of the last line of
And here's proof (the -P parameter shows you the physicalcurrent working directory - i.e. resolves symlinks, whereas pwd without any parameters normally shows you the logical current working directory):
Unfortunately there's no way to eat the cake and have it too! I suggest you exercise caution with symlinks, especially if you're programming... this kind of setup can have unintended consequences!
Try this simpler example:
The first pwd outputs the "fake" logical path. The second pwd, with the -P outputs the actual physical path. Any symbolic links I wish to create under "root_of_everything" will have to be relative to the physical, not logical path.