Why have symbolic links between files in folders at same depth?
Am I correct in my understanding of symbolic links there?
Yes. Symbolic link, as the name implies acts as a link to a file/folder located elsewhere. Accessing the link is generally equivalent to accessing the file/folder.
If so, why would you create symbolic links in one folder (
/usr/local/opt
) to binaries in another folder (/usr/local/Cellar
) that is at a similar location/depth in the file structure?
If you'll carefully observer in this particular case, the binaries corresponding to all the various installed formulae are in different folders. There's a separate folder corresponding to each installed formula under /usr/local/Cellar
. Within the folder for formula is a folder containing version number, followed by installed files for the formula.
Via symlink under /usr/local/opt
, the installation location for all the formula are brought in one place.
For knowing the exact purpose of why /usr/local/opt
folder is created by Homebrew, you can refer to this post on Stack Overflow:
- https://stackoverflow.com/questions/35337601/why-is-there-a-usr-local-opt-directory-created-by-homebrew-and-should-i-use-it
I’m not that familiar with Homebrew, but the pattern you are describing is one I’ve seen other software installers use: it makes various binaries available under one directory, while making it easy to tell what software package they come from. The screenshot below is from an article about Homebrew, and shows a listing of the directory “/usr/local/opt”, with the various symbolic links pointing to subdirectories of “/usr/local/Cellar”. While most of the binaries point to a package with the same name, this is for example not the case for “npm”, where the symbolic link makes it easy to tell that it is part of the same package as “node” and “nodejs”. Symbolic links are used here for filesystem organization purposes: all of the files of a package are kept together in one place, while making them available from other directories as needed.
You may also be interested in an example of how symbolic links are sometimes used without even pointing to a different directory at all. Take a look at the output of ls -l /usr/bin | grep vim
, which should show various links pointing to “vim” in the same directory, for example “ex” and “view”. As the man page for “vim” explains:
Vim behaves differently, depending on the name of the command (the exe-
cutable may still be the same file).
vim The "normal" way, everything is default.
ex Start in Ex mode. Go to Normal mode with the ":vi" command.
Can also be done with the "-e" argument.
view Start in read-only mode. You will be protected from writing
the files. Can also be done with the "-R" argument.