brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ln -sfn repoints a symlink to a folder instead of creating a link inside it

Linux
~/tips/tip-linux-ln-sfn

ln -sf /srv/app/releases/v2 /srv/app/current

Replaces a symlink that points to a directory, rather than adding a link inside that directory.

A current symlink that points to the latest release folder is a neat way to switch versions. Updating it with ln -sf looks right, and it does something odd that I think catches almost everyone once.

When the link name you give is a symlink to a directory, ln treats it like that directory by default. So this doesn’t change current at all:

ln -sf /srv/app/releases/v2 /srv/app/current

Instead it creates a new link called v2 inside the old release folder, because current still points there. current keeps pointing at v1 and you’re left with a stray link in a place you didn’t look. The -n flag (--no-dereference) tells ln to treat the existing symlink as a plain file, so -f replaces it:

ln -sfn /srv/app/releases/v2 /srv/app/current
ls -l /srv/app/current

The ls -l shows the arrow pointing at v2 now. I’d use the same pattern for a latest link to a backup folder or a config directory you switch between.

The caveat: -T (--no-target-directory) is the stricter option, and it also refuses to put the link inside a real directory that happens to have the same name. The manual calls -n the weaker of the two and says it has no effect when both are given. If current could ever end up being an actual folder rather than a link, ln -sfT gives you an error instead of a surprise.

Further reading: ln(1) manual page, GNU coreutils: ln invocation

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link