Symlink use cases: Shortcuts, App config files

Image for post
Image for post

Shortcuts 🥱

Symbolic links are handy for shortcuts in your file explorer.

Maybe you have shortcuts on your desktop to your favorite folders, or maybe you symlink your active projects in your home directory for quick bash access.

For example:

ln -s /User/alex/Apps/2020/appthing /User/alex/appthing

This will create a shortcut to ~/Apps/2020/appthing directly in my home dir, i.e. cd ~/appthing

Notice how I used absolute paths when creating the symlink.

You don’t need to use absolute paths, but it’s the best way to stay out of trouble. But as you’ll see below, there are times when relative symlinks are useful.

App config files 🤔

This one is more interesting. We are going to use symlinks to expose a swappable, version controlled config collection to the application.

First I’ll explain the project (it’s very simple) and then I’ll talk about why I like it.

Here’s the folder structure of my project:

➜  appthing tree .
.
├── app.py
├── configs
│ └── cfg_1.py
└── config.py -> configs/cfg_1.py

As you can see, there’s a configs folder containing some configuration cfg_1.py. This is linked to config.py (a symlink file which, by the way, can be committed to version control):

cd appthing
ln -s configs/cfg_1.py config.py

Notice how I used relative paths when creating the symlink. This way the project can be cloned onto other computers or servers without the symbolic link breaking.

Here’s what my config file cfg_1.py looks like:

➜  appthing cat config.py
class config:
HELLO = "World"

and here’s what app.py looks like:

➜  appthing cat app.py
from config import config
print(config.HELLO)

Running it goes like this:

➜  appthing python app.py
World

The point of using a symlink is to be able to quickly change the config file without needing to copy or move the current one.

Say that you needed another instance of this app running (with different config options) — you could make the new config file configs/cfg_2.py and quickly start testing them by swapping the symlink ln -s configs/cfg_2.py config.py

  • We can maintain a version controlled set of configuration files
  • App configurations can easily be exchanged (e.g. cloning multiple instances of an app to run on different configs)
  • Working version of config file is same as app config. No need to copy the file after saving edits (i.e. cp configs/cfg_1.py config.py ⛔)

Conclusion

Thanks for reading. Let me know what you think in a comment below or on twitter.

Image for post
Image for post
https://twitter.com/agalea91/status/1283259664239431682

Now get back to your projects, they are missing you ;)

https://alexgalea.ca/

Written by

Python Data Engineer, MSc. Physics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store