how to change a symbolic link instead of deleting it?

时间:2021-10-15 07:48:04

someone told me that if i want to change a symbolic link after it is created, i should use the -f options,so i try:

andy@AndyMacBookPro:/usr/local/webdata$ ll union
lrwxr-xr-x  1 andy  admin  10  3 30 15:34 union@ -> union_dev1
andy@AndyMacBookPro:/usr/local/webdata$ ln -sfv union_branches union
union/union_branches -> union_branches
andy@AndyMacBookPro:/usr/local/webdata$ ll union
lrwxr-xr-x  1 andy  admin  10  3 30 15:34 union@ -> union_dev1
andy@AndyMacBookPro:/usr/local/webdata$

but as you see , i can’t change the symbolic link at all, and i have another symbolic link under the /usr/local/webdata/union_dev1/ named union_branches.

so i decide to find the answer by myself, i find this article:
http://superuser.com/questions/749545/how-to-change-a-symbolic-link-to-a-directory-to-another-target

and someone use the :

ln -sfnv

to make it happen!great!

and the -n options means :

Use -n/–no-dereference (“treat LINK_NAME as a normal file if it is a symbolic link to a directory”):

这是我之前没有在意的,之前我只知道-f选项可以强制覆盖,但是linux这里的bug是如果发现目标是目录并且已经存在软链的话就会使用在此目录下创建软链而不是将此目录作为一个普通文件去处理,下面是更全面和专业的解释:

That is, ln -s target link will work like ln -s target link/target if link links to a directory.

Most Linux command tools will try to dereference symbolic links before applying their action, otherwise symbolic links would be mostly just confusing and almost useless. I guess the original ln authors thought consistency was more important than a certain amount of simplicity (with the risk of being confusing to people used to other tools) in this case.

其实也就是说,如果你的软链目标恰巧是一个文件而不是目录的话,ln -sf是可以的,但是如果不幸是一个目录,那么就会出现你意料之外的结果。

我在我的OSX系统man ln我本地的手册是这样的:

LN(1)                     BSD General Commands Manual                    LN(1)

NAME
     link, ln -- make links

SYNOPSIS
     ln [-Ffhinsv] source_file [target_file]
     ln [-Ffhinsv] source_file ... target_dir
     link source_file target_file

DESCRIPTION
     The ln utility creates a new directory entry (linked file) which has the
     same modes as the original file.  It is useful for maintaining multiple
     copies of a file in many places at once without using up storage for the
     ``copies''; instead, a link ``points'' to the original copy.  There are
     two types of links; hard links and symbolic links.  How a link ``points''
     to a file is one of the differences between a hard and symbolic link.

     The options are as follows:

     -F    If the target file already exists and is a directory, then remove
           it so that the link may occur.  The -F option should be used with
           either -f or -i options.  If none is specified, -f is implied.  The
           -F option is a no-op unless -s option is specified.

     -h    If the target_file or target_dir is a symbolic link, do not follow
           it.  This is most useful with the -f option, to replace a symlink
           which may point to a directory.

     -f    If the target file already exists, then unlink it so that the link
           may occur.  (The -f option overrides any previous -i options.)

     -i    Cause ln to write a prompt to standard error if the target file
           exists.  If the response from the standard input begins with the
           character `y' or `Y', then unlink the target file so that the link
           may occur.  Otherwise, do not attempt the link.  (The -i option
           overrides any previous -f options.)

:

也可以参考这里的解释。