I thought I’d seen an answer to this question before, but after searching here and google I just can’t find it.
How do you replace an existing symlink with a copy of the file it points to? IE copy the source file over the link.
Start with this
/path/to/file.txt
/link/path/file.txt -> /path/to/file.txt
Then call something like copy('/path/to/file.txt', '/link/path/file.txt'); and get this:
/path/to/file.txt
/link/path/file.txt
The reason i can’t just use copy() is that it doesn’t seem to support overwriting a symlink with its source file as it thinks they’re the same thing and gives the error “XXXXX and YYYYY are identical (not copied)”
Is there a method like copy which can overwrite a symlink with a copy of its source file?
I don’t think it’s possible with a single command. Here’s a solution that doesn’t involve renaming (you should probably add in error checking):
If you find yourself doing this a lot, you may find it worthwhile to wrap this in a handy function.