Symlink aliasing files in subdirectories without changing current directory

But something about the syntax is perplexing and counter to what I would expect.

The arguments for ln, in the form that you're using it, are:

ln [OPTION]... [-T] TARGET LINK_NAME (1st form)

The perplexing, unintuitive thing is that when you're creating a symlink, the target argument for ln isn't expected to be a path to a file, but rather the contents of the symlink to be created. If you think about it for a moment, it's obvious that it has to be that way. Consider:

$ echo foo >foo
$ ln -s foo bar1
$ ln -s $PWD/foo bar2
$ cat bar1
foo
$ cat bar2
foo
$ ls -l bar1 bar2
lrwxrwxrwx 1 matt matt  3 Dec 29 16:29 bar1 -> foo
lrwxrwxrwx 1 matt matt 29 Dec 29 16:29 bar2 -> /home/matt/testdir/foo

In that example I create 2 symlinks, named "bar1" and "bar2", that point to the same file. ls shows that the symlinks themselves have different contents, though - one contains an absolute path, and one contains a relative path. Because of this, one would continue working even if it were moved to another directory, and the other wouldn't:

$ mv bar2 /tmp
$ cat /tmp/bar2
foo
$ mv bar1 /tmp
$ cat /tmp/bar1
cat: /tmp/bar1: No such file or directory

So, considering that we must be able to make both relative and absolute symlinks, and even to create broken symlinks that will become un-broken if the target file is later created, the target argument has to be interpreted as freeform text, rather than the path to an already-existing file.

If you want to create a file named deploy/resources.php that links to deploy/resources.build.php, you need to decide if you want to create an absolute symlink (which is resilient against the symlink being moved, but breaks if the target is moved), or a relative symlink (which will keep working as long as both the symlink and the target are moved together and maintain the same relative paths).

To create an absolute symlink, you could do:

$ ln -s $PWD/deploy/resources.build.php deploy/resources.php

To create a relative one, you would first figure out the relative path from the source to the target. In this case, since the source and target are in the same directory relative to one another, you can just do:

$ ln -s resources.build.php deploy/resources.php

If they weren't in the same directory, you would need to instead do something like:

$ ln -s ../foo/f bar/b

In that case, even though foo and bar are both in your current directory, you need to include a ../ into the ln target because it describes how to find f from the directory containing b.

That's an extremely long explanation, but hopefully it helps you to understand the ln syntax a little better.


You can create the link in a subshell, as follows:

  (cd deploy && ln -s resources.build.php resources.php && cat resources.php)

When the subshell ends execution, you will find yourself still in the correct directory.

Alternatively, you may try

 ln -s resources.build.php deploy/resources.php

which also works, neglecting to include in the CL the fact that the file resources.build.php is not in the directory where you are issuing the command, but is in fact inside ./deploy.