Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to use cp to overwrite a symbolic link
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
javeree
Guru
Guru


Joined: 29 Jan 2006
Posts: 453

PostPosted: Thu Apr 15, 2021 10:45 am    Post subject: How to use cp to overwrite a symbolic link Reply with quote

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.
Back to top
View user's profile Send private message
freke
l33t
l33t


Joined: 23 Jan 2003
Posts: 980
Location: Somewhere in Denmark

PostPosted: Thu Apr 15, 2021 11:04 am    Post subject: Reply with quote

You can use
Code:
cp --remove-destination source symlink
That will remove symlink before doing the cp.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum