cp command to overwrite the destination file which is a symbolic link

Dien Nguyen picture Dien Nguyen · Feb 21, 2012 · Viewed 14.1k times · Source

Does cp command have any option to overwrite the destination file which is a symbolic link?

The problem is as follows:

[dthnguyen@dthnguyen test]$ ls -l
total 8
-rw-rw-r--. 1 dthnguyen dthnguyen 5 Feb 21 09:07 a.txt
lrwxrwxrwx. 1 dthnguyen dthnguyen 7 Feb 21 08:55 b.txt -> ./a.txt
-rw-rw-r--. 1 dthnguyen dthnguyen 5 Feb 21 08:55 c.txt
[dthnguyen@dthnguyen test]$ cp c.txt b.txt

After do the copy, a.txt has the content of c.txt, b.txt still links to a.txt. The expected result is a.txt holds the old content, b.txt is a new regular file that has the same content as c.txt.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Feb 21, 2012

Tell cp to remove it first.

cp --remove-destination c.txt b.txt