24

Is there any way to force cp (Bash 4.2.5, Ubuntu 12.04) to copy onto a dangling symlink?

cp a-file path/to/danling/symlink/a-file
cp: not writing through dangling symlink `path/to/danling/symlink/a-file`

cp -f seems to be impotent in this case and results in the same message.

muru
  • 207,228

2 Answers2

35

Make cp remove the target file before copying:

$ 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

From man cp:

--remove-destination
      remove  each existing destination file before attempting to open
      it (contrast with --force)
muru
  • 207,228
4

Just use unlink theSymLink where theSymLink is the actual symlink, then try again