Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

shell

cp

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.

like image 469
Dien Nguyen Avatar asked Feb 21 '12 02:02

Dien Nguyen


People also ask

How do you overwrite a symbolic link?

Overwriting Symlinks If you try to create a symbolic link that already exists , the ln command will print an error message. To overwrite the destination path of the symlink, use the -f ( --force ) option.

Does cp command overwrite files?

By default, cp will overwrite files without asking. If the destination file name already exists, its data is destroyed. If you want to be prompted for confirmation before files are overwritten, use the -i (interactive) option.

Does cp overwrite directory?

Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown. To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.

How do I remove a symbolic link from a directory?

To remove a symbolic link, use either the rm or unlink command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.


1 Answers

Tell cp to remove it first.

cp --remove-destination c.txt b.txt 
like image 63
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 14:09

Ignacio Vazquez-Abrams