0

I have read here and in few books that making symbolic links using relative pathway is preferred because if you move link and file, it won't break. I wondering if we should always create relative pathway symbolic links? Like for example if I want to link /etc/passwd to current directory, should I use absolute or relative path?

ln -rs /etc/passwd passwd-symlink will create the relative pathway link but is this necessary?

3
  • I don't understand the question. You do what you need - these things have different use cases. If you want a link to be relative, you use a relative link; if you need it to be absolute, you use an absolute link. Dec 19, 2021 at 0:59
  • For example, you have a link "configs-enabled/config.txt" that points to another file that's always in "../configs-available/`; in this case, you want a relative link. You have config file that should always point to /etc/config.txt? Then you clearly want an absolute path. This becomes more clear when you realize that not all processes necessarily see the same file system hierarchy (e.g. due to Linux namespaces/containers, or chroot, or due to bind mounts, or due to the same volume being mounted at a different place on a different machine), so /etc/config.txt might be different! Dec 19, 2021 at 1:04
  • @MarcusMüller The question is under what scenarios would you use a relative vs. absolute pathway. I am asking for a decision process of using one over other.
    – Cruise5
    Dec 19, 2021 at 1:19

1 Answer 1

2

There is probably not one clear answer for this, and there are many instances where it doesn't particularly matter if a symlink target is relative or absolute.

It is probably a good idea to use a relative symlink if any of the following apply, because the link will remain valid in these situations:

  • if the target is in the same directory tree and the whole tree could be moved
  • If the target is in the same filesystem, and it gets mounted somewhere else in some future mount (perhaps in a rescue live environment or a container or on another system)

However an absolute link might be better if any of the following apply

  • the target is in a radically different directory, where the right number of ../ sequences might not be clear
  • you want to point to a very specific file that is not relative to the current directory (for example, a config file in /etc maybe)
  • if you want to be very clear what file this points to, when a realtive link might turn into a maze of symlinks pointing to symlinks or targets that traverse symlinks to directories...

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.