It seems like it should be simple to symlink one file to a new file in a subdirectory…. ….without moving subdirectories. But something about the syntax is perplexing and counter to what I would expect. Here's a test case:
mkdir temp
cd temp
mkdir deploy
echo "Contents of the build file!" > deploy/resources.build.php
ln -s deploy/resources.build.php deploy/resources.php
cat deploy/resources.php #bad symlink
This just creates a broken symlink!
I am running this in a build environment setup script, so I want to avoid changing the current working directory if at all possible.
ln -s deploy/resources.build.php resources.php
cat deploy/resources.php
Also doesn't work because it creates the symlink in the temp directory instead of the deploy subdirectory.
cd deploy
ln -s resources.build.php resources.php
cd ..
This works, but I'd prefer to know how to do it without changing directories.
Using a full path like:
/home/whatever/src/project/temp/stuff/temp/deploy/resources.build.php
Works, but is unweildy and somewhat impractical, especially in a build environment where all the project stuff might be different between builds, and the like.
How can I create a symlink between two files in a subdirectory, without moving into that subdirectory and out of it, and while giving the new file "alias" a new name?
Best Answer
The arguments for
ln
, in the form that you're using it, are: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: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: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:
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:
If they weren't in the same directory, you would need to instead do something like:
In that case, even though
foo
andbar
are both in your current directory, you need to include a../
into theln
target because it describes how to findf
from the directory containingb
.That's an extremely long explanation, but hopefully it helps you to understand the
ln
syntax a little better.