How to delete or replace an already created symbolic link?

I am working on my project using network simulator2. I installed it and everything is fine. I attempted to create a symbolic link between this installation and /usr/bin, so I could invoke the software by running ns from the command-line. Namely, I ran:

sudo ln -s /home/vinaychalluru/ns-allinone-2.34/ns-2.34/ns /usr/bin/ns

which generated the following output:

ln: creating symbolic link '/usr/bin/ns': File exists


How can I delete the already created symlink or can I replace it with any other commands?

Asked By: Vinay

||
sudo rm -r /usr/bin/ns

sudo ln -s /home/vinaychalluru/ns-allinone-2.34/ns-2.34/ns /usr/bin/ns
Answered By: PraweĊ‚

ln has -f switch that ‘forces’ a symlink to be created whether it exists or not.

sudo ln -sf /home/vinaychalluru/ns-allinone-2.34/ns-2.34/ns /usr/bin/ns
Answered By: finley

To add to all answers above, a symbolic link can be treated as a regular file in many cases (the link, not the target).

rm on a symbolic link will remove it. If the link is owned by root, you will need to sudo.

You should be able to rm /usr/bin/ns or rather sudo rm /usr/bin/ns don’t forget ownership.

Answered By: Dmitriy Likhten

I tried ln -sf while replacing but it didn’t work for me, but doing ln -sfn directory link_name as root worked.

Answered By: user712092