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.

  • Did you try using cp -f instead? – Rohith Madhavan Nov 19 '14 at 18:49
  • @RohithMadhavan yep; same message (I just updated the question) – Marcus Junius Brutus Nov 19 '14 at 18:58

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

From man cp:

      remove  each existing destination file before attempting to open
      it (contrast with --force)
| improve this answer | |

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

| improve this answer | |
  • 4
    This will work, but note that unlink has the same effect as (and thus no advantage compared to) the more commonly used rm. In particular, like rm foo, unlink foo will delete a file foo even when it is a regular file and not a symbolic link. Using unlink instead of rm (or mv --remove-destination ...) does not guard against accidental data loss. – Eliah Kagan Apr 27 '15 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.