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

前端 未结 7 1735
陌清茗
陌清茗 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"
    }
    
    0 讨论(0)
  • 2020-12-12 11:25

    Yes, you can!

    $ ln -sfn source_file_or_directory_name softlink_name
    
    0 讨论(0)
  • 2020-12-12 11:27

    Just in case it helps: there is a way to edit a symlink with midnight commander (mc). The menu command is (in French on my mc interface):

    Fichier / Éditer le lien symbolique
    

    which may be translated to:

    File / Edit symbolic link
    

    The shortcut is C-x C-s

    Maybe it internally uses the ln --force command, I don't know.

    Now, I'm trying to find a way to edit a whole lot of symlinks at once (that's how I arrived here).

    0 讨论(0)
  • 2020-12-12 11:28

    It is not necessary to explicitly unlink the old symlink. You can do this:

    ln -s newtarget temp
    mv temp mylink
    

    (or use the equivalent symlink and rename calls). This is better than explicitly unlinking because rename is atomic, so you can be assured that the link will always point to either the old or new target. However this will not reuse the original inode.

    On some filesystems, the target of the symlink is stored in the inode itself (in place of the block list) if it is short enough; this is determined at the time it is created.

    Regarding the assertion that the actual owner and group are immaterial, symlink(7) on Linux says that there is a case where it is significant:

    The owner and group of an existing symbolic link can be changed using lchown(2). The only time that the ownership of a symbolic link matters is when the link is being removed or renamed in a directory that has the sticky bit set (see stat(2)).

    The last access and last modification timestamps of a symbolic link can be changed using utimensat(2) or lutimes(3).

    On Linux, the permissions of a symbolic link are not used in any operations; the permissions are always 0777 (read, write, and execute for all user categories), and can't be changed.

    0 讨论(0)
  • 2020-12-12 11:29

    Wouldn't unlinking it and creating the new one do the same thing in the end anyway?

    0 讨论(0)
  • 2020-12-12 11:36

    AFAIK, no, you can't. You have to remove it and recreate it. Actually, you can overwrite a symlink and thus update the pathname referenced by it:

    $ ln -s .bashrc test
    $ ls -al test
    lrwxrwxrwx 1 pascal pascal 7 2009-09-23 17:12 test -> .bashrc
    $ ln -s .profile test
    ln: creating symbolic link `test': File exists
    $ ln -s -f .profile test
    $ ls -al test
    lrwxrwxrwx 1 pascal pascal 8 2009-09-23 17:12 test -> .profile
    

    EDIT: As the OP pointed out in a comment, using the --force option will make ln perform a system call to unlink() before symlink(). Below, the output of strace on my linux box proving it:

    $ strace -o /tmp/output.txt ln -s -f .bash_aliases test
    $ grep -C3 ^unlink /tmp/output.txt 
    lstat64("test", {st_mode=S_IFLNK|0777, st_size=7, ...}) = 0
    stat64(".bash_aliases", {st_mode=S_IFREG|0644, st_size=2043, ...}) = 0
    symlink(".bash_aliases", "test")        = -1 EEXIST (File exists)
    unlink("test")                          = 0
    symlink(".bash_aliases", "test")        = 0
    close(0)                                = 0
    close(1)                                = 0
    

    So I guess the final answer is "no".

    EDIT: The following is copied from Arto Bendiken's answer over on unix.stackexchange.com, circa 2016.

    This can indeed be done atomically with rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states:

    If newpath refers to a symbolic link the link will be overwritten.

    In the shell, you would do this with mv -T as follows:

    $ mkdir a b
    $ ln -s a z
    $ ln -s b z.new
    $ mv -T z.new z
    

    You can strace that last command to make sure it is indeed using rename(2) under the hood:

    $ strace mv -T z.new z
    lstat64("z.new", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
    lstat64("z", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
    rename("z.new", "z")                    = 0
    

    Note that in the above, both mv -T and strace are Linux-specific.

    On FreeBSD, use mv -h alternately.

    Editor's note: This is how Capistrano has done it for years now, ever since ~2.15. See this pull request.

    0 讨论(0)
提交回复
热议问题