Remember to delete the symbol link before copy to it

Today there was an interesting thing happened, during I using Xorg
libX.so which is compiled by myself to test xiiim input method,I tested twice.

The first time, xiiimp.so.2 had not been complied. I just created a symbol link to /usr/openwin/lib/locale/common/xiiimp.so.2  in path
/usr/local/lib/X11/locale/common:

ln -s  /usr/openwin/lib/locale/common/xiiimp.so.2 xiiimp.so.2

The second time, I compiled a new xiiimp.so.2,  and  copy  it into /usr/local/lib/X11/locale/common to use it directly.
 

After twice test, I foud my gnome-terminal cannot launch any more. By a little research, I know the reason is that 
xiiimp.so.2 which I complied was still using by xiiim’s process.

Do you see any problem?
The problem is from operation "copy": Because the target file is a symbol link file, so /usr/openwin/lib/locale/common/xiiimp.so.2 was replaced by the new xiiimp.so.2 which I compiled.

Make a sample example:
1)Create two test files:
$echo a>a
$echo b>b

2)Create symbol link c:
$ln -s a c
check file:
-rw——-   1 xx staff          2  5月 22日 16:02 a
-rw——-   1 xx staff          2  5月 22日 16:02 b
lrwxrwxrwx   1 xx staff          1  5月 22日 16:03 c -> a

$cat c
a
$cat a
a
$cat b
b

3)Copy file:
$cp b c

4)Check file after copy:
$cat a
b
$cat c
b

Summary:
When you are ready to copy file that target file is a symbol link, remember to delete the symbol link before copy to it.

2 thoughts on “Remember to delete the symbol link before copy to it

  1. Thank you for you tips🙂.
    But “cp -i” is interactive. cp prompts for confirmation whenever the copy would overwrite an existing target.
    What result I want to get is :
    do not overwrite “a” and same time let c become a file which has same content with b.
    Do you know if have any tip can do this?

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s