强制 cp 在悬空的符号链接上复制

Mar*_*tus 20 symbolic-link cp

有没有办法强制cp(Bash 4.2.5,Ubuntu 12.04)复制到悬空的符号链接上?

cp a-file path/to/danling/symlink/a-file
cp: not writing through dangling symlink `path/to/danling/symlink/a-file`
Run Code Online (Sandbox Code Playgroud)

cp -f 在这种情况下似乎无能为力并导致相同的消息。

mur*_*uru 29

cp复制之前删除目标文件:

$ ln -s /random/file f              
$ cp -f a f                  
cp: not writing through dangling symlink ‘f’
$ cp --remove-destination a f
$ diff a f && echo yes
yes
Run Code Online (Sandbox Code Playgroud)

来自man cp

--remove-destination
      remove  each existing destination file before attempting to open
      it (contrast with --force)
Run Code Online (Sandbox Code Playgroud)