javeree Guru
Joined: 29 Jan 2006 Posts: 453
|
Posted: Thu Apr 15, 2021 10:45 am Post subject: How to use cp to overwrite a symbolic link |
|
|
I want to copy a file, overwriting another file that is a symlink. My objective is that the symlink destination should not be overwritten and that the symlink file is replaced with a regular file. I don't seem to be able to do that with the cp command:
Quote: | $ echo 'This is the source' > source
$ echo 'This should not be overwritten' > no-overwrite
$ ln -s no-overwrite symlink
$ ls -l source no-overwrite symlink
-rw-r--r-- 1 javeree users 31 15 apr 12:32 no-overwrite
-rw-r--r-- 1 javeree users 19 15 apr 12:31 source
lrwxrwxrwx 1 javeree users 12 15 apr 12:32 symlink -> no-overwrite
$ wc source no-overwrite symlink
1 4 19 source
1 5 31 no-overwrite
1 5 31 symlink
3 14 81 total
$ cp source symlink
$ ls -l source no-overwrite symlink
-rw-r--r-- 1 javeree users 19 15 apr 12:33 no-overwrite
-rw-r--r-- 1 javeree users 19 15 apr 12:31 source
lrwxrwxrwx 1 javeree users 12 15 apr 12:32 symlink -> no-overwrite <<---- This should not be a symlink anymore, but a regular file with a copy of 'source'
$ wc source no-overwrite symlink
1 4 19 source
1 4 19 no-overwrite <<---- I don't want this file to be overwritten
1 4 19 symlink
3 12 57 total
$ cat no-overwrite
This is the source |
As you can see cp does not overwrite the symlink, but does the equivalent thing of cat source > symlink
What I want to happen is the equivalent of rm symlink; cp source symlink
Is this possible with cp alone, so that I don't need to check first if the destination is existing and is a symlink ?
I could not find any obvious thing in the man file. |
|