3

Context

As I was navigating through the file-system to find the persisted files from a mounted docker volume, I found it difficult to unravel the unending ls/cd/tree of directories only to find more symlinks... Maybe this is the wrong question to ask since my search is not producing a satisfying answer

Question

Why do certain programs or systems rely on symlinks?

Is there a history lesson here, similar to Rob Pike's story about the origins of dotfiles?

I've read the wikipedia pages on file systems and symbolic links, which thoroughly explain the what, but not the why

Examples

On macOS the following directories are entirely symlinks

  • /usr/local/bin
  • /usr/local/var/homebrew/linked
  • 1
    who said that they rely on symlinks? You can copy the files instead of creating symlinks and they should still work, unless they expect some files in a symlinked directory to be the same – phuclv Jan 12 '23 at 11:34
  • 1
    Yes, I'm assuming reliance since those directories are full with symlinks. Another way to ask this: Would systems break if you copied the files instead of using a symlink? Given the answer below my guess is that you would create silos that are less maintainable over time – will-wright-eng Jan 12 '23 at 19:04

1 Answers1

3

Many systems rely on symlinks essentially for handling default settings.

The relationship between a program name and the code that should run is not always biunivocal (a 1 to 1 relationship) :

a 1 to many : Because different implementations of whatever commonly used program can coexist on a system, and, users and historical scripts just do not need/want to care… and simply fire some common command name, administrators need to symlink :

Just think of the most basic sh command. Just many shells exist bash, csh, ksh, zsh… pffff… and just many of the associated binaries can coexist on the system. Which one of these should be launched when simply asked for sh ?

On my system the arbitrary decision is as simple as :

616457 0 lrwxrwxrwx 1 root root 4 14 mai     2021 /bin/sh -> bash

Of course we immediately think of sh but the same would happen with just many commonly used command names (awk, bzip2, bc, cpio, gzip, lex, tar, yacc…) On my system :

616450 0 lrwxrwxrwx 1 root root 15  8 juil.   2020 /bin/awk -> ../usr/bin/gawk
484973 0 lrwxrwxrwx 1 root root 10  8 juil.   2020 /usr/bin/yacc -> yacc.bison
484674 0 lrwxrwxrwx 1 root root 4 27 nov.   02:14 /usr/bin/lex -> flex

Of course, all this goes similarly when different versions of the same program coexist. Think principally of compilers, language interpreters, not to say running kernel's source code. On my system :

484470 0 lrwxrwxrwx 1 root root 43 26 nov.   23:43 /usr/bin/gcc -> /usr/x86_64-pc-linux-gnu/gcc-bin/10.4.0/gcc
485883 0 lrwxrwxrwx 1 root root 19  7 déc.  01:41 /usr/src/linux -> linux-5.4.225-0707a

a many to 1 : The other need to rely on symlinks is for very special filesystems such as /sys where the information resides in one unique pseudo-file which is handy to access through several paths when searching for it.

MC68020
  • 7,981
  • Ah! This makes sense, thank you. I never thought of symlinks as being their own abstraction layer for components of the operating system. Also, would you mind adding a little more to what you mean by biunivocal in this context? I'm not familiar with it's usage in computing... is this what you meant by "different versions of the same program coexist" – will-wright-eng Jan 12 '23 at 19:01
  • there's no reliance in the case of sh. You can cp /bin/bash /bin/sh and no one would complain. Or you can install a true sh into /bin/sh – phuclv Jan 13 '23 at 00:49
  • 1
    @will-wright-eng : biunivocal ? I mean a 1 to 1 relationship. In my first example taken between a generic program name and the "real" binary is a 1 to many relationship. Hence the need for a default. In my last example (/sys like pseudo fs) its a many to 1 relation ship. – MC68020 Jan 13 '23 at 16:08
  • @phuclv : "install a true sh"… what do you mean ? THE Thompson shell ? Errr ? Who would want/risk that under anything else than a pre 1975 AT&T Unix ©™® ? – MC68020 Jan 13 '23 at 16:18
  • @MC68020 obviously bash and sh are completely different things. If you make sh a symlink to bash then bash will run in sh mode which isn't sh at all but a few features of bash will be enabled – phuclv Jan 14 '23 at 03:02
  • sh is the Bourne shell described in the POSIX standard, and bash is the Bourne again shell shell – phuclv Jan 14 '23 at 04:09
  • 1
    @phuclv The Bourne shell and POSIX sh are actually slightly different (and that's aside from POSIX sh being an abstract entity). – muru Jan 16 '23 at 09:30