Get parent of symbolic link instead of original folder

bashfinder

I have a project with node_modules folder that contains a symbolic link to another project. The problem that inner project takes configs from parent project (etc ../../../config) and thus cannot find it because of the relative path starting from the original folder instead of a symbolic link.

Example:

$ cd ~
$ mkdir -p a/b/c
$ ln -s a/b/c d
$ cd d
$ ls ../

Will display content of b folder. But I need to display listing of ~ directory. How to do it?

Best Answer

Unfortunately, this is not how symbolic links work.

From the man page (man ln)

The ln utility creates a new directory entry (linked file) for the file name specified by target_file. The target_file will be created with the same file modes as the source_file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the “copies”; instead, a link “points” to the original copy.

Emphasis mine

So, how do you get around this? Use an environment variable. For example:

$ mkdir -p /a/b/c
$ FOO='/a/b/c'
$ cd ~
$ cd $FOO
$ pwd
/a/b/c

Note, you should use absolute paths when doing this; using relative paths can and will have unintended consequences.