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.
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.
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 .
│ └── 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):
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
HELLO = "World"
and here’s what
app.py looks like:
➜ appthing cat app.py
from config import config
Running it goes like this:
➜ appthing python app.py
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⛔)
Thanks for reading. Let me know what you think in a comment below or on twitter.
Now get back to your projects, they are missing you ;)