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

前端 未结 7 1736
陌清茗
陌清茗 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:37

    Just a warning to the correct answers above:

    Using the -f / --force Method provides a risk to lose the file if you mix up source and target:

    mbucher@server2:~/test$ ls -la
    total 11448
    drwxr-xr-x  2 mbucher www-data    4096 May 25 15:27 .
    drwxr-xr-x 18 mbucher www-data    4096 May 25 15:13 ..
    -rw-r--r--  1 mbucher www-data 4109466 May 25 15:26 data.tar.gz
    -rw-r--r--  1 mbucher www-data 7582480 May 25 15:27 otherdata.tar.gz
    lrwxrwxrwx  1 mbucher www-data      11 May 25 15:26 thesymlink -> data.tar.gz
    mbucher@server2:~/test$ 
    mbucher@server2:~/test$ ln -s -f thesymlink otherdata.tar.gz 
    mbucher@server2:~/test$ 
    mbucher@server2:~/test$ ls -la
    total 4028
    drwxr-xr-x  2 mbucher www-data    4096 May 25 15:28 .
    drwxr-xr-x 18 mbucher www-data    4096 May 25 15:13 ..
    -rw-r--r--  1 mbucher www-data 4109466 May 25 15:26 data.tar.gz
    lrwxrwxrwx  1 mbucher www-data      10 May 25 15:28 otherdata.tar.gz -> thesymlink
    lrwxrwxrwx  1 mbucher www-data      11 May 25 15:26 thesymlink -> data.tar.gz
    

    Of course this is intended, but usually mistakes occur. So, deleting and rebuilding the symlink is a bit more work but also a bit saver:

    mbucher@server2:~/test$ rm thesymlink && ln -s thesymlink otherdata.tar.gz 
    ln: creating symbolic link `otherdata.tar.gz': File exists
    

    which at least keeps my file.

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