Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    bash

    Safely and atomically change a symlink target

    RyanBy RyanNovember 21, 2012Updated:May 15, 20151 Min Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    It can be tricky to change the target of a symlink in a production environment where that symlink is accessed often and unpredictably. The normal method is to remove the old symlink, then create the new one. If you have a symlink that is accessed often or unpredictably there is a chance that something will try to access it between these operations and have bad results.

    The easiest method for changing a symlink is to use “ln -f” which will unlink the old symlink then create a new one. Again this is not atomic but will work in most traditional situations.

    $ ls -l current
    current -> old_target
    $ ln -fs new_target current
    $ ls -l current
    current -> old_target

    To safely change the target of symbolic link, you will first need to create a temporary symlink to the new target then rename it atomically using mv to the old symlink.

    $ ln -s new_target current_tmp && mv current_tmp current

    Since mv is an atomic operation, the symlink will always exist and there is no race condition.

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleTransfer your ssh public key to a remote machine
    Next Article Netflix now working on Ubuntu Linux

    Related Posts

    Netflix now working on Ubuntu Linux

    Transfer your ssh public key to a remote machine

    Fix OpenGL: ChoosePixelFormat SketchUp error in WINE

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.