1

How does the relative paths work in ln (-s or not)? For example if I type ln -s foo bar/banana.txt what does this mean? What is foo relative to? Because it doesn't seem to be relative to the current path. Also is it different if I remove -s or not? I've tested it out and the result doesn't make sense to me, and the man page doesn't seem to explain this. Could anyone explain?

2

It's different with and without -s.

With -s in:

ln -s path/to/file some/dir/link

path/to/file is set as the symlink target of some/dir/link (or some/dir/link/file if link was a directory).

A symlink is a special type of file which /contains/ a path (can be any array of non-0 bytes, some systems even allow an empty string) which is the target of the symlink. ln sets it to the first argument.

Upon resolving the link (when the link is used later on), the path/to/file path will be relative to the directory the link is (hard-)linked to (some/dir here when some/dir/link is accessed via its some/dir/link path).

Note that the path/to/file doesn't need to exist at the time the ln command is run (or ever).

While in:

ln path/to/file some/dir/link

It's similar to:

cp path/to/file some/dir/link

The path/to/file is relative to the current working directory of the process running ln.

Nothing stops you from creating several (hard-)links to a symlink. For example:

$ mkdir -p a b/c b/a
$ ln -s ../a b/L # b/L a symlink to "a"
$ ln b/L b/c/L

b/L and b/c/L are the same file: same inode but linked to two different directories. They are both symlinks with target ../a. But when b/L is resolved, that points to ./a while when b/c/L, that points to ./b/a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.