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

Viewed 149140

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 unlinking the old one and creating a new one?

The POSIX standard does not. Solaris 10 does not. MacOS X 10.5 (Leopard) does not. (I'm tolerably certain neither AIX nor HP-UX does either. Judging from this list of Linux system calls, Linux does not have such a system call either.)

Is there anything that does?

(I'm expecting that the answer is "No".)


Since proving a negative is hard, let's reorganize the question.

If you know that some (Unix-like) operating system not already listed has no system call for rewriting the value of a symlink (the string returned by readlink()) without removing the old symlink and creating a new one, please add it — or them — in an answer.

8 Answers

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"
}

You can modify the softlink created once in one of the two ways as below in Linux

  1. one is where you can remove existing softlink with rm and again create new softlink with ln -s command .
  2. However this can be done in one step , you can replace existing softlink with updated path with "ln -vfns Source_path Destination_path" command.

Listing initial all files in directory

$ ls -lrt 
drwxrwxr-x. 3 root    root      110 Feb 27 18:58 test_script
$

Create softlink test for test_script with ln -s command.

$ ln -s test_script test
$ ls -lrt
drwxrwxr-x. 3 root    root      110 Feb 27 18:58 test_script
lrwxrwxrwx. 1 root    root       11 Feb 27 18:58 test -> test_script
$

Update softlink test with new directory test_script/softlink with single command

$ ln -vfns test_script/softlink/ test
'test' -> 'test_script/softlink/'
$

List new softlink location

$ ls -lrt
lrwxrwxrwx. 1 root    root       21 Feb 27 18:59 test -> test_script/softlink/
$

ln --help

-v, --verbose print name of each linked file

-f, --force remove existing destination files

-n, --no-dereference treat LINK_NAME as a normal file if it is a symbol

-s, --symbolic make symbolic links instead of hard links

Related