Bash

Symlink Force-Update

admin by @admin ADMIN
Jun 20, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`ln -s` fails if the target already exists. `ln -sf` is the idempotent variant — useful in deploy scripts that point a "current" symlink to a fresh release directory.
Bash
Raw
# Atomic deploy pattern:
#   /opt/myapp/releases/20250312-141500
#   /opt/myapp/current -> releases/20250312-141500

deploy() {
    local release="releases/$(date -u +%Y%m%d-%H%M%S)"
    mkdir -p "/opt/myapp/$release"
    # ... unpack the new build into /opt/myapp/$release ...

    # Atomic switch — readers see either the old or the new, never a torn state
    ln -sfn "$release" "/opt/myapp/current"
    echo "Now serving: $(readlink /opt/myapp/current)"
}

# -s = symbolic, -f = force overwrite, -n = treat existing symlink as a file
# (not as a directory to descend into — without -n, ln would create
# /opt/myapp/current/release instead of updating the symlink)
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.