How to delete or replace an already created symbolic link?

157,898

Solution 1

sudo rm -r /usr/bin/ns

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

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
157,898

Related videos on Youtube

Vinay
Author by

Vinay

Python | Azure | Linux | Cloud Computing | Machine Learning

Updated on September 17, 2022

Comments

  • Vinay
    Vinay over 1 year

    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?

    • aviggiano
      aviggiano almost 11 years
      I was trying to do sudo ln -s destination source, and of course it bugged. Thanks for the answer.
  • finley
    finley over 13 years
    the above fails if ns is a folder - in which case use the rm -rf (recursive force switches) - in which case you might as well use the -sf switches for ln.
  • Vinay
    Vinay over 13 years
    @finley Yes, you are right. Incase ns is a folder, it can't be deletd. It is now ambiguos to me that, can we create a symbolic link to a folder.?
  • Praweł
    Praweł over 13 years
    yes, we can create a symbolic link to a folder. If it's a folder, you can do: sudo rm -r /usr/bin/ns
  • Vinay
    Vinay over 13 years
    @Pawel I created a symlink to folder succesfully. I can even remove as u said above. But, when i type fold [created symlink for a folder] name in terminal it gives an error. What i actually thought was by creating a symlink to a folder and just typing the link name terminal would open that in nautilus. I am not sure whether it is possible or not..?
  • Praweł
    Praweł over 13 years
    @Vinaychalluru if you want to open that folder, try typing: cd fold - it will open in a terminal. If you want to open it in nautilus, type: nautilus fold
  • Vinay
    Vinay over 13 years
    I did sudo ln -s /home/vinayc/ns-allinone-2.34/ns-2.34/ /usr/bin/fol >> cd fol which gave me an error bash: cd: fol: No such file or directory. It's because there is no folder fol in /home/vinayc/. ? You said that, we can create a symbolic link to a folder. Should I make changes in the command.?
  • Praweł
    Praweł over 13 years
    @Vinaychalluru no, that is not the point :) sudo ln -s /home/vinayc/ns-allinone-2.34/ns-2.34/ /usr/bin/fol and then cd fol or nautilus fol. These are 3 separate commands!
  • Christopher Markieta
    Christopher Markieta over 8 years
    Without -n I was getting an infinite loop problem. Thanks!
  • Trevor Hickey
    Trevor Hickey about 8 years
    I'd recommend avoiding this method. Misusing "rm -rf" with an ill-formatted input could be catastrophic. Prefer "ln -sf" instead
  • Rony Varghese
    Rony Varghese almost 7 years
    This should be the accepted answer.