Can you change what a symlink points to after it is created?

前端 未结 7 1751
陌清茗
陌清茗 2020-12-12 10:52

Does any operating system provide a mechanism (system call — not command line program) to change the pathname referenced by a symbolic link (symlink) — other than by unlinki

7条回答
  •  不知归路
    2020-12-12 11:21

    Technically, there's no built-in command to edit an existing symbolic link. It can be easily achieved with a few short commands.

    Here's a little bash/zsh function I wrote to update an existing symbolic link:

    # -----------------------------------------
    # Edit an existing symbolic link
    #
    # @1 = Name of symbolic link to edit
    # @2 = Full destination path to update existing symlink with 
    # -----------------------------------------
    function edit-symlink () {
        if [ -z "$1" ]; then
            echo "Name of symbolic link you would like to edit:"
            read LINK
        else
            LINK="$1"
        fi
        LINKTMP="$LINK-tmp"
        if [ -z "$2" ]; then
            echo "Full destination path to update existing symlink with:"
            read DEST
        else
            DEST="$2"
        fi
        ln -s $DEST $LINKTMP
        rm $LINK
        mv $LINKTMP $LINK
        printf "Updated $LINK to point to new destination -> $DEST"
    }
    

提交回复
热议问题