Update a symlink with ln -sfn instead of breaking it

Repoint an existing symbolic link on Linux using ln -sfn to avoid nested links inside the old target directory.

LinuxlinkpathsymlinkLow risk
Run ln -sfn NEW_TARGET LINK_NAME to atomically repoint an existing symlink, using -n when the link currently points at a directory.

01Command

Command
$ ln -sfn /opt/app/releases/v2.1.0 /opt/app/current

02What it does

Plain ln -sf TARGET LINK is a classic trap: if LINK is a symlink to a directory, the new link gets created inside that directory instead of replacing it. The -n flag makes ln treat the link itself as an ordinary file, and -f removes it before creating the replacement — together -sfn is the idiom used for the releases/current-style symlink switching common in deployments.

03Risk

RiskLow risk
!Warning: ln -sf on a directory symlink without -n silently creates the new link inside the old target; verify with ls -l afterwards.

04Undo

Undo
$ ln -sfn /opt/app/releases/v2.0.9 /opt/app/current

05Sources

  • ln(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    Evidence: -f removes existing destination files, -n treats LINK_NAME as a normal file if it is a symlink to a directory, -v prints each link made
  • ln(1P) — POSIX manual page
    man7.org · technical · 2026-09-17
    Evidence: POSIX defines -f to remove existing destination paths before creating the link

Last verified: 2026-09-17 · Category: Files & Folders