Symlink use cases: Shortcuts, App config files

Alex Galea
3 min readJul 15, 2020

--

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

This is useful because

  • 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.

https://twitter.com/agalea91/status/1283259664239431682

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

https://alexgalea.ca/

--

--

Alex Galea
Alex Galea

Written by Alex Galea

Python Data Engineer, MSc. Physics

No responses yet